As far as I understood, Hibernate is applying a simple strategy to determine whether an object is new or exists in the database: if it has an ID set, then it exists, otherwise it is new. This status determines whether saveOrUpdate would result in an INSERT or UPDATE statement.
In many cases for me, this is not sufficient. For example, a customer entity may have its primary id in the database and this would serve as hibernate's id. However, there might also be a combination of other attributes that identify the customer => "alternate key(s)", for example name and city.
Now I would like to have saveOrUpdate (or some method I would implement) to check if there is an alternate key, if this key is complete (name and city are set) and then use sql SELECT to determine if the entity already exists. If so, then this is an update, otherwise this is an insert.
Does Hibernate provide any support for this? Is there a way I could specify the alternate key(s) in the mapping files (or through annotations or whatever) and access them later in my code?
I would not like to explicitly program this logic for a particular class, rather define the keys and have a single spot that uses them to apply the logic as above.
Any ideas?
|