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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate object id consisting of two colums?
PostPosted: Mon Apr 21, 2008 1:30 pm 
Newbie

Joined: Sun Apr 06, 2008 2:49 pm
Posts: 17
Hi,

Assume we have two entities A and B and many to many relationship.

A
long aID
string aaa;

B
long bID;
string bbb;

AB
long aID (pfK)
long bID (pfK)
string addInfo1;
string addInfo2;
long something; (fk)

And there are corresponding c# classes with exactly the same public properties and names (i.e. table A is class A).

Now should I add a new column or this is OK:

<composite-id class="AB"> //name = ???
<key-many-to-one name="aID" class="A" column="aID"/>
<key-many-to-one name="bID" class="B" column="bID"/>
//do i need key-property?
</composite-id>


Top
 Profile  
 
 Post subject: many-to-many
PostPosted: Mon Apr 21, 2008 2:07 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Hibernate is able to define a many-to-many relationship through a 3rd table, only if that 3rd table contains just the PKs of both tables.
In a case like yours, on the contrary, where there is more information on the "relationship table", I would advise trying to think that "many-to-many" relationship into two different relationship.

1) A "one to many" relationship between A and AB
2) A "many to one" relationship between AB and B

I would further advice, in order to make things simpler for Hibernate, to create an additional, single-field PK for the AB table.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 2:13 pm 
Newbie

Joined: Sun Apr 06, 2008 2:49 pm
Posts: 17
well, isn't it exactly what i showed above?



1) A "one to many" relationship between A and AB
2) A "many to one" relationship between AB and B

As AB has additional info apart from foreign keys it has to ba an entity...
So:
A is one to many AB
B is one to many AB

AB is many to one A
AB is many to one B

And the single PK for AB os rather out of the question, that's why i asked is it OK:

<composite-id class="AB"> //name = ???
<key-many-to-one name="aID" class="A" column="aID"/>
<key-many-to-one name="bID" class="B" column="bID"/>
//do i need key-property?
</composite-id>

is it?

I am rather not asking about relationships as is it simple, but hibernate id which is required for every entity..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 4:40 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
No, it isn't. You are creating a many-to-many table with a composite id.
I am telling you to create one with a single id, and then relate the other 2 tables to it in the manner I previously described.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: ok
PostPosted: Tue Apr 22, 2008 12:50 pm 
Newbie

Joined: Sun Apr 06, 2008 2:49 pm
Posts: 17
Ok I can do that (add additional column with PK) but I still don't understand why my approach is wrong, you are telling that what I am doing is wrong but not telling why.
I overrided hashcode and equals methods and did small nunit test saving AB class to database with no problems so where is the problem?

And mappings are:
A
<id name="id">
<generator class="native" />
</id>
<set name="ABList" table="AB">
<key column="aID" />
<one-to-many class="AB" />
</set>

and for B:

B
<id name="id">
<generator class="native" />
</id>
<set name="ABList" table="AB">
<key column="bID" />
<one-to-many class="AB" />
</set>

and for AB there is no many-to-one collection mapping, only:

<composite-id class="AB">
<key-many-to-one name="aID" class="A" column="aID"/>
<key-many-to-one name="bID" class="B" column="bID"/>
</composite-id>

and some string propersies


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