-->
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: Initailizing collection problem
PostPosted: Mon Apr 30, 2007 7:36 am 
Newbie

Joined: Mon Apr 30, 2007 7:05 am
Posts: 1
Hi all,

I have a one-to-many relationship between Wachlist class and Profile class
(a watchlist contains one or more profiles)

the mapping is like that :

Watchlist.hbm.xml
<map name="profiles" inverse="true"
cascade="all-delete-orphan">
<key column="WATCHLIST_ID" />
<map-key column="STOCK_ID" type="long" />
<one-to-many class="Profile" />
</map>

Profile.hbm.xml
<many-to-one name="watchlist" class="Watchlist" column="WATCHLIST_ID" not-null="true" unique-key="WATCHLIST_UNIQUE" cascade="lock"/>

in code i want to add a profile to a watchlist so i worte the following line
Code:
watchlist.getProfiles().put(stock.getId(),profile);


when using hibernate 3.1 CR1 the above line of code initialize the map and add the new profile successfully.

But when using hibernate 3.2.0 GA the above line of code just INITAILIZE the map and ignore the put method

to overcome this, i explicitly initialize the map
Code:
Hibernate.initialize(watchlist.getProfiles());
watchlist.getProfiles().put(stock.getId(),profile);


So I wonder if there any problem initializing the map and putting a new element at the same time when using hibernate hibernate 3.2.0 GA?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 9:59 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
On your bidirectional association you need to manage both sides in Java (Hibernate takes care of this relationally). I highly recommend you add a method to Watchlist that does this, i.e.

Code:
getProfiles().add(..);
profile.setWatchlist(this);


http://www.hibernate.org/hib_docs/v3/re ... usingbidir

You should probably even make the getter/setter for the Map !public unless you really need to replace the entire Map.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


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.