-->
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.  [ 4 posts ] 
Author Message
 Post subject: ManyToOne and OneToMany
PostPosted: Tue Jan 08, 2008 1:35 pm 
Newbie

Joined: Fri Jul 27, 2007 4:24 pm
Posts: 5
Hi,

I have two entities: Province and City. City has a Province many-to-one field and Province has a one-to-many set of Cities. When I create a new City, and associate it with a Province, the set of cities in the associated Province instance does not get updated within the current session. It doesn't bother me much in a web app, because I open a new session for each request, but in case of a long running desktop app it's a bit inconvenient.

Is there a property that I set to make hibernate update the cache on each update, or is it he cascade property?

Thanks,

Pavel


Top
 Profile  
 
 Post subject: Re: ManyToOne and OneToMany
PostPosted: Wed Jan 09, 2008 6:14 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am a little confused by your question. If I understand correctly each time you are adding a city to a province you update both sides of the relation which means you have loaded a province from either cache or database and you have, therefore, loaded the list of cities in that province. As a result of update hibernate will reflect the changes to database and updates its underlying cache. The only trick here is when used with JTA you will have to hook up hibernate with the JTA transaction manager so that hibernate knows when everything was happily sent to database and it can keep the changes in its cache.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 10:31 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
You may have run into the issue that while FOREIGN KEYS are bi directional in the database in your Java code you have to explicitly set both sides of the relationship.

So just doing City.setProvince(Province) is not enough you also have to do a Province.getCities().add(City). Of course I don't know how your code exactly looks so the above is only an approximation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 12:18 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
I think you have to do 2 things:
- set the "inverse=true" attribute ("mappedBy" in annotations)
- set a cascade option

Carlo


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