These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Can insert data to CONNECTION TABLE in many-to-many?
PostPosted: Thu Aug 17, 2006 10:21 am 
Newbie

Joined: Tue Jul 18, 2006 2:29 pm
Posts: 10
I have 3 tables.

tblEquiment:
-EquipId
-EquipSerialNumber
....

tblAccount:
-AccountId
-AccountName
....

And I have a connection table for many-to-many:
tblAccount_X_Equipment:
-EquipId
-AccountId
-StartDateUse
-EndDateUse
....

If tblAccount_X_Equipment only has 2 fields: EquipId,AccountId. It's a relation many-to-many, you can easy do it.

But How can I Insert data to StartDateUse and EndDateUse fields for management Equipment and keep the ralation many-to-many....


Top
 Profile  
 
 Post subject: Found solution
PostPosted: Fri Aug 18, 2006 12:50 am 
Newbie

Joined: Tue Jul 18, 2006 2:29 pm
Posts: 10
I have found the solution for this problem.

From tblAccount_X_Equipment table, you create two many-to-one relation.
First to tblEquiment; Second to tblAccount.
And "biensu^r", you must create one-to-many from tblEquiment, tblAccount to tblAccount_X_Equipment.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 6:52 am 
Beginner
Beginner

Joined: Sun Jun 19, 2005 2:21 pm
Posts: 21
Can you show some sample code and hbm mappings? I am also interested in this kind of relationships. Do you use a third object corresponding to tblAccount_X_Equipment table?

Thanks,
Robert


Top
 Profile  
 
 Post subject: My example
PostPosted: Fri Aug 18, 2006 11:38 am 
Newbie

Joined: Tue Jul 18, 2006 2:29 pm
Posts: 10
If you have known about one-to-many relation, it's easy to understand.

TblAccount.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="TrainingCenter.Bussiness.TblAccount, TrainingCenter" table="tblAccount">
<id name="AccountId" column="AccountId" type="Int64" length="8" unsaved-value="0" >
<generator class="identity" />
</id>
<property name="UserName" column="UserName" type="String" />
<property name="PassWord" column="PassWord" type="String" />
<property name="Email" column="Email" type="String" />
<property name="Permission" column="Permission" type="Byte" />
<property name="Status" column="Status" type="Int32" />
<bag name="AccountEquipment" inverse="true" lazy="true" cascade="all" fetch="select">
<key column="AccountId" />
<one-to-many class="TrainingCenter.Bussiness.tblAccount_X_Equipment, TrainingCenter" />
</bag>
</class>
</hibernate-mapping>

TblEquipment.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="TrainingCenter.Bussiness.TblEquipment,TrainingCenter" table="tblEquipment">
<id name="EquipId" column="EquipId" type="Int64" length="8" unsaved-value="0" >
<generator class="identity" />
</id>
<property name="SerialNumber" column="SerialNumber" type="String" />
<property name="DateAdded" column="DateAdded" type="DateTime" />
<property name="Status" column="Status" type="Byte" />
<property name="Notes" column="Notes" type="String" />

<many-to-one name="EquipType" column="TypeId" not-null="true" fetch="select" cascade="save-update" />

<bag name="AccountEquipment" inverse="true" lazy="true" cascade="all" fetch="select">
<key column="EquipId" />
<one-to-many class="TrainingCenter.Bussiness.tblAccount_X_Equipment, TrainingCenter" />
</bag>

</class>
</hibernate-mapping>

TblAccount_X_Equipment.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="TrainingCenter.Bussiness.TblAccount_X_Equipment, TrainingCenter" table="tblAccount_X_Equipment">
<id name="AccEquiId" column="AccEquiId" type="Int64" length="8" unsaved-value="0" >
<generator class="identity" />
</id>

<property name="StartTime" column="StartTime" type="DateTime" length="8" />
<property name="EndTime" column="EndTime" type="DateTime" length="8" />
<property name="Notes" column="Notes" type="String" length="200" />

<many-to-one name="AccountParent" column="AccountId" not-null="true" fetch="select" cascade="save-update" />
<many-to-one name="EquipmentParent" column="EquipId" not-null="true" fetch="select" cascade="save-update" />

</class>
</hibernate-mapping>

------------------------------
Function to Insert data:

public void Add_Account_Equipment(long accountid,long equipid,DateTime starttime,DateTime endtime,string notes)
{
ITransaction transaction;
try
{
transaction=session.BeginTransaction();
Bussiness.TblAccount account =(Bussiness.TblAccount) session.Get(typeof(Bussiness.TblAccount),accountid);
Bussiness.TblEquipment equipment =(Bussiness.TblEquipment) session.Get(typeof(Bussiness.TblEquipment),equipid);

Bussiness.TblAccount_X_Equipment accequip = new Bussiness.TblAccount_X_Equipment();

accequip.AccountParent =account;
accequip.EquipmentParent=equipment;

accequip.StartTime=starttime;
accequip.EndTime=endtime;
accequip.Notes=notes;

account.AccountEquipment.Add(accequip);
equipment.AccountEquipment.Add(accequip);

session.Save(accequip);
transaction.Commit();
session.Disconnect();

}
catch(Exception ex)
{
throw ex;
}
finally
{
session.Close();
}
}[/u]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.