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.  [ 3 posts ] 
Author Message
 Post subject: Tables with Composite Keys
PostPosted: Wed Jul 18, 2007 2:10 pm 
Newbie

Joined: Wed Jul 11, 2007 11:23 am
Posts: 7
Can someone provide or point me to a complete code sample, with an hbm.xml and a .cs file that references a table witha composite key? I've tried reading the documentation; however I am sorry to say it's over my head. For example, the statement in the doc that says the persisent class MUST override Equals() and GetHashCode() to implement composite identifier equality... is over my head.

Suppose I have a table named MyTable with three columns, each nvarchar(10) called ColumnA, ColumnB, and ColumnC where ColumnA and ColumnB make up the composite key.

I will try to keep plugging away trying to figure out how to do this. However an help will be appreciated.

I apologize sincerely for my newbieness.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 11:55 am 
Newbie

Joined: Mon Jul 16, 2007 5:38 pm
Posts: 9
If you are not sure how to override the Equals and GetHashCode then you probably do not need to implement anything crazy so all you need to override them is:

public override bool Equals(object obj)
{
return base.Equals(obj);
}

doing the same for GetHashCode. These are placed in your class file.


Here is an example that uses composite keys.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
<class name="Record" table="tblOne">
<composite-id>
<key-property name="PropA" column="ColA"/>
<key-property name="PropB" column="ColB"/>
</composite-id>

<property name ="PropC" column="ColC"/>
<bag name="ChildRecord" lazy="false" fetch="subselect" table="tblTwo">
<key>
<column name="ChildColA"/>
<column name="ChildColB"/>
</key>
<one-to-many class="ChildRecord"/>
</bag>
</class>

<class name ="ChildRecord" table="tblTwo">
<id name="Id" column="AnotherColumn" access="nosetter.camelcase" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name ="SomeProperty" column="ColumnD"/>
</class>
</hibernate-mapping>

If you are trying to do a subselect (as shown here) with a composite key you are going to run into problems. NHibernate will generate invalid sql. However the fetch="Select" works just fine, slow but it works.
If you happen to find a way to get subselect or joins to work with a composite key let me know because I have not been able to get it to work yet.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 11:57 am 
Newbie

Joined: Mon Jul 16, 2007 5:38 pm
Posts: 9
If you are not sure how to override the Equals and GetHashCode then you probably do not need to implement anything crazy so all you need to override them is:

public override bool Equals(object obj)
{
return base.Equals(obj);
}

doing the same for GetHashCode. These are placed in your class file.


Your .cs file will just be some getters and setters( which are used as the properites for the hbm files) along with the Equals and GetHashCode.

Here is an example that uses composite keys.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
<class name="Record" table="tblOne">
<composite-id>
<key-property name="PropA" column="ColA"/>
<key-property name="PropB" column="ColB"/>
</composite-id>

<property name ="PropC" column="ColC"/>
<bag name="ChildRecord" lazy="false" fetch="subselect" table="tblTwo">
<key>
<column name="ChildColA"/>
<column name="ChildColB"/>
</key>
<one-to-many class="ChildRecord"/>
</bag>
</class>

<class name ="ChildRecord" table="tblTwo">
<id name="Id" column="AnotherColumn" access="nosetter.camelcase" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name ="SomeProperty" column="ColumnD"/>
</class>
</hibernate-mapping>

If you are trying to do a subselect (as shown here) with a composite key you are going to run into problems. NHibernate will generate invalid sql. However the fetch="Select" works just fine, slow but it works.
If you happen to find a way to get subselect or joins to work with a composite key let me know because I have not been able to get it to work yet.

Don't under estimate the documentation it is pretty good, sometimes it takes a couple of re-reads to understand it but it is overall very helpful.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.