• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate : child changes but parent doesnt know

 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

-j
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,

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.

Post-Transactions
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.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !?

J
 
David Madouros
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,

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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


Mark
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic