my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes Avoid resetting in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Avoid resetting in hibernate" Watch "Avoid resetting in hibernate" New topic
Author

Avoid resetting in hibernate

Nishnt Rayn
Greenhorn

Joined: Oct 23, 2008
Posts: 14
Hi all,
Is there a way to specify in hiberate not to set properties that have been set previously in the object.
To be specific,
1) I have an object that has been prefilled with subset of properties in a different section of code.
2) After that, i use hibernate to fill the rest of the properties
I don't want to overwrite the properties set in step 1. i use the values in the database as defaults only with not specified in step 1.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate will not set any properties, with a few exceptions (any generated ID, a discriminator, the version etc. ). You can specify defaults in the mapping file and these will behave like defaults in the database. This is only useful for databases that don't have their own default constraints. If you are using a databse that has default constraints just use them.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nishnt Rayn
Greenhorn

Joined: Oct 23, 2008
Posts: 14
Hi,
Thanks for the reply but i don't want to hard code my defaults in the mapping file because it can change depending on ID (primary key). Any other suggestion ???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your defaults change by ID? That sounds like business rules - you can't easily implement this sort of logic in the database (triggers are the only way). Is there any reason you don't just default it in your code?

If you want Hibernate to do it, you'll need to write some interceptor code. Have a look at the JavaDocs for org.hibernate.Interceptor to see what you'll need to do.

I'm always hesitant to do this sort of thing. A good DAO layer should be fairly dumb, adding Hibernate specific functionality makes it harder to swap persistance technologies, or debug how values are being set.
Nishnt Rayn
Greenhorn

Joined: Oct 23, 2008
Posts: 14
When i said "Default changed by ID" i meant a table with schema like [ID,Column1,Column2].
my bean object has the properties: [ID,Property1,Property2]. Based on ID i can select the default values from the table. !
Seems like interceptor is a relevant alternative in this situation but dreading the situation where i have to manage the interceptors.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Avoid resetting in hibernate