Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoid resetting in hibernate

 
Nishnt Rayn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nishnt Rayn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic