Reputation: 2335
I have an established application built on Eclipselink / MySQL. Each persistent object has been given a unique key that is generated as a sequence (unique within the database - not just the corresponding table). We now need to add a second unique key that is sequential. This key is generated with reference to a sequence variable stored in a configuration table. That is, read the variable, add 1, use as key and update variable.
The problem is that this process is failing intermittently. At this stage, we are not sure why (but are investigating) So my question is: is there any theoretical reason not to have two unique keys on an object (table)? Is there a preferred mechanism for doing this?
Upvotes: 0
Views: 91
Reputation: 446
What about implementing @PrePersist callback for this entity. It can do 'index table' select/update and set proper value in the entity. As Chris already mentioned, model itself does not support multiple sequencies in it.
Another option is to do it manually in some DAO method within a single transaction - retrieve value from table, set it (with regular setter), persist entity and commit/rollback depending on the result.
Upvotes: 1