This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate mapping and versioning issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate mapping and versioning issues" Watch "Hibernate mapping and versioning issues" New topic
Author

Hibernate mapping and versioning issues

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hi all,
I am having a few problems with hibernate, one we wont go into.
I have two tables, a User and a UserRole table, now the UserRole is the parent of User, so there is a one-to-many relationship there, but between User and UserRole it is a one-to-one relation ship which I am trying to map thus:


The problem is, it dosnt seem to load the UserRole with the User (I have tried with and without the lazy attribute).
Looking at the logs it seems to be spitting out the right SQL:

so any ideas why it is not getting the required object?
The userRoleOID is teh forgin key to the UserRole table.

My second question, I am versioning objects in my DB, as can be seen by the version property in the above mapping, I have some "complex" sql to ensure the latest version of the object is being returned.

Does hibernate always return the latest version of an object in a "get" request?

Thank
Gavin
[ May 09, 2007: Message edited by: Gavin Tranter ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336



My second question, I am versioning objects in my DB, as can be seen by the version property in the above mapping, I have some "complex" sql to ensure the latest version of the object is being returned.

Does hibernate always return the latest version of an object in a "get" request?



Hibernate will return the version that exists in the database when the get request is made, unless it already has an older version in the session.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
Hi,

I think what the paul, saying is correct but while updating it will create an problem....ther will be not an dirty checking...
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
Hi,

I think what paul, saying is correct but while updating it will create an problem....their will be not an dirty checking... i am confused here.....

can clearly my doubts pl
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by saranga rao:
Hi,

I think what paul, saying is correct but while updating it will create an problem....their will be not an dirty checking... i am confused here.....

can clearly my doubts pl


Yes you sound confused. Hibernate's versioning is an implementation of optimistic locking, if you get an 'old' version from the session (i.e. another porcess has updated the row outside Hibernate's control) and try to updated it Hibernate will throw an exception when the session is closed or flushed.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Does that mean I can make my selects simplier and remove the extra logic that ensured teh newest version was selected?

G
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I don't see how you can implement anything in a select statement that ensures you have the latest version of a row in your app (unless you are using pessimistic locking)? Can you show us what you mean?
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hi,
perhaps I mean the latest persisted version :S.

given a table:


So OID and VID make up a composite key, were by every time the object is updated, a new row is created with the same OID as the original but with an incremented VID.

You would general only want the row with the given OID that had the highest VID, this would ensure teh latest vesion of the "object".

The Query i was using was:


I think my question is null and void at the mo, as i am using mySQL and it dosnt have a sequence generator, and i cant get SequenceStyleGenerator in hibernate to work. So, I can only have one row with a given OID, and would have to implement my own load, save and update methods to use this with hibernate.

I think I know understand that version to hibernate is used to determine if the "cahced" version of an object is newer/older then the currently persisted version, and if persistance should take place, and not for object versioning in the way i meant (Keeping a history of the object). Or something like that

was probably overkill for my little app anyway

Thanks
G
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ah right, "versioning" equals archiving data. Now it makes sense! Yes this is a different kind of versioning than Hibernate supplies (which is an optimistic lock implementation). There's nothing out the box that can help you with archiving data I'm afraid.
[ May 09, 2007: Message edited by: Paul Sturrock ]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thanks Paul,
To be honest, I didnt expect there to be, and after the issues with hibernates SequenceStyleGenerator, I have put it (the archived db idea) in the bin, and am just going to let hibernate behave in a default manner, I dont really have the time to play to get hibernate to work with mySQL with a sequence generator.

Oh well, next time maybe

Thanks again for you help
Gavin
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate mapping and versioning issues