File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes hibernate : child changes but parent doesnt know Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate : child changes but parent doesnt know " Watch "hibernate : child changes but parent doesnt know " New topic

hibernate : child changes but parent doesnt know

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hi Guys :

I have a hibernate issue which should be fairly common.

class Car
Set<Passengers> passengers;
//getters and setters.

Now, when I modify one of the passengers (call it p) , and I call session.refresh(p), the car's list of passengers does not reflect this modification.

How can i make sure that an object's children will be modified in such a way as to ensure that any collection's to which they belong to stay in sync ?

David Madouros

Joined: Mar 08, 2005
Posts: 18

If you could post your hibernate transaction boundaries it would be very helpful. Without them the best I can do is make assumptions. Here goes:

Transaction #1
You get a Car out of the database.

Transaction #2
You get a Passenger our of the database.
You update (and save) the Passenger.

You refresh the Passenger. (Transaction #3?)
You iterate through the collection of passengers in Car and don't see the update.

Based on these assumptions, I would suggest two things to keep your objects in sync:
1) get the passenger object out of the car's collection of passengers and update that. Then save either the passenger or the car.
- OR -
2) call refresh on car instead of passenger.

Note: A transaction, in this case, is the opening and closing of a Hibernate Session.

Hope that helps.

David Madouros<br />SCJP 1.4
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hi : The transaction is being created
and commited in the exact same method that
does the update.

My problem is more high level : If I have to worry
about what order I insert the objects, then how am
I better off than I used to be with plain old JDBC.

That is --- If a relationship which is mapped to Class X, in the data model is not reflected after a refresh(x) (where x is an instance of X),
then this is a serious flaw in hibernates architecture, dont you think !?

David Madouros

Joined: Mar 08, 2005
Posts: 18

I'm inclined to say no it's not a flaw in hibernate's architecture.

I don't understand how you can have a passenger in a car, update the passenger and not see the change reflected in the passenger if it all takes place in a single transaction.

The only way that I can see this happenning is if you go out of your way to retrieve the car and it's passengers and store car somewhere (perhaps in an HTTPSession). Then at some later point, you retrieve a passenger from the database (that also happens to be a passenger in car),update that passenger, and expect the passenger in car in session to be automagically updated. It makes sense to me that the passenger in the car is stale. If this is the case, then I agree that hibernate does not offer a better solution for this problem than straight jdbc.

Honestly, I'm feeling kind of dense at the moment, cause I don't understand where you're coming from. I've used hibernate for several projects both personal and professional and I don't recall this being an issue. Not to say that I know it all (I most certainly do not), just that I don't get it.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I think this is an issue where you haven't committed your transaction yet, and therefore the database still has the old data, and when you call refresh, Hibernate queries the database and gets the old values.

So I have two points.

1. You are in the same Unit of Work where you changed the Passenger object which is in the "Car", so why do you have to refresh, your Java Objects already have the most up to date data. Now, if you try to do a session.flush, which will send the change back to the database, (As long as you modified the Passenger object when it is in a persistent state), then call refresh, you should see the changes.

2. Why do you need to call refresh? I have yet to come across a use case where I needed to do that, unless I rolled everything back, and wanted my Java objects to go back to the exact state of the database.

And you had this question "then how am
I better off than I used to be with plain old JDBC. "

How about removing that 30% of code that is all JDBC and a pain to maintain? Regardless of this issue, that alone would make me jump away from JDBC. Way too much code to write and maintain, and it isn't my Business Logic. And having to write code to transform the ResultSets into my actual Java Objects, and having to figure out how to do Java inheritance transformations, etc. And having to implement my own Caching mechanism, and dirty reading, so that I know exactly what I need to send back to the database after my unit of work. Handling Transactions on my own, especially two phase commits.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Are you cascading your 'refresh' of the parent onto the child? (by declaring the cascade property in the metadata) Parents don't automagically pull everything from the database.

Anyway, its beyond me how you can be doing this in a single transaction and not seeing the change? You need to show code because that makes totally no sense. Its not even a hibernate issue. Probably you are thinking hibernate can override basic Java data structure which it at least, does not.
[ March 12, 2007: Message edited by: Mr. C Lamont Gilbert ]
I agree. Here's the link:
subject: hibernate : child changes but parent doesnt know
jQuery in Action, 3rd edition