-->
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.  [ 2 posts ] 
Author Message
 Post subject: Newbie question referencing the same class from two fields
PostPosted: Sat Dec 30, 2006 2:20 pm 
Newbie

Joined: Sat Dec 30, 2006 2:17 pm
Posts: 1
I'm strageling with this for some time now, so please forgive me if it’s a stupid question.
I have two classes,
User with ID and name
And Message with text ,fromUser and toUser.
I write HBM files and run hbm2java and hbm2ddl to create .java and .sql files.
My question is: how do I define this relationship in the HBM so that in the java files I will have something like:
List<Message> list_1 = aUser.getSentMessages();
And
List<Message> list_2 = aUser.getReceived Messages();

What I tried is in Message.hbm.xml:
<many-to-one
name="fromUser"
class="User"
not-found="ignore"
fetch="join"
not-null="false" >
<column name="FROM_USER_ID" />
</many-to-one>

<many-to-one
name="toUser"
class="User"
not-found="ignore"
fetch="join"
not-null="false" >
<column name="TO_USER_ID" />
</many-to-one>

and in User.hbm.xml:
<set name="sentMessages" lazy="false" inverse="true">
<key column="USER_ID"/>
<one-to-many class="Message"/>
</set>

but this is clearly wrong.

Any suggestions will be highly appreciated

Thanks,
~Z


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 4:45 am 
Regular
Regular

Joined: Thu Aug 17, 2006 4:50 am
Posts: 55
Location: Mallorca
In the inverse relation (the one in user), you specify the foreign key column (that is, the column in the Message table that references the user entry).

it should be something like:

Code:
<set name="sentMessages" lazy="false" inverse="true">
<key column="FROM_USER_ID"/>
<one-to-many class="Message"/>
</set>

<set name="received Messages" lazy="false" inverse="true">
<key column="TO_USER_ID"/>
<one-to-many class="Message"/>
</set>



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