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.
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.
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.
Joined: Oct 23, 2008
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.