-->
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: :newbie: Easy question regarding composite objects
PostPosted: Thu Dec 15, 2005 1:49 pm 
Newbie

Joined: Sun Dec 11, 2005 1:51 pm
Posts: 4
Hi everybody,

I'm new to hibernate and already have a problem.
I have two classes say "Continent" and "Country". Country class contains one private field fo "Continent" type in order to refer to some country.

Ok what is my problem?!?

When I instantiate an country object and save it in database all goes well. For example, i have country object with name "USA" which have "America" object in continent field. So I managed saving that composite object into datbase tables. But, when I instantiate a "Canada" object and associate it with "America" continent and save class into database I HAVE TWO "America" records in COUNTRY table.

Wanted behaviour will be that Hibernate recognize that I'm refering to the same "America" object and to be smart enough to link "Canada" object with existing "America" object instead of creating new one.


I hope, this simple question will wring some credits to you

Best regards
Milicic Marko


Top
 Profile  
 
 Post subject: many-to-one
PostPosted: Thu Dec 15, 2005 2:05 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Continent should be mapped ad many-to-one
http://www.hibernate.org/hib_docs/v3/re ... -manytoone

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 2:27 pm 
Newbie

Joined: Sun Dec 11, 2005 1:51 pm
Posts: 4
Actualy, I already made that kind of mapping.

<hibernate-mapping package="datamanagers">
<class name="Country" table="COUNTRY">
<id name="id" column="ID" type="java.lang.Long">
<generator class="native"/>
</id>

<property name="countryName" column="COUNTRY_NAME" type="java.lang.String"/>
<property name="countryISO" column="COUNTRY_ISO" type="java.lang.String"/>
<many-to-one name="continent" column="CONTINENT_ID" class="Continent" not-null="true"/>
</class>
</hibernate-mapping>




<hibernate-mapping package="datamanagers">
<class name="Continent" table="CONTINENT">
<id name="id" column="ID" type="java.lang.Long">
<generator class="native"/>
</id>

<property name="continentName" column="CONTINENT_NAME" type="java.lang.String"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: code please
PostPosted: Thu Dec 15, 2005 2:38 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Please post your code between session open and close.
I suspect that you simply create a new Continent object with name “America” and put this in the Country object, that will cause creation of the new Continent record with the same name but different ID.
If you want to reuse Continent, the you should try to find existing Continent with the given name and then
country.setContinent( theExistingOne );

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 11:39 am 
Newbie

Joined: Sun Dec 11, 2005 1:51 pm
Posts: 4
Thank you, you were right!


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.