Hi all,
I'm trying to wrap Hibernate around an existing data model, which as expected has its ups and downs.
My current sticking point is that one of the entity tables has a quasi-temporal model; no rows are ever deleted or updated; instead, the "is_current" column is set to false (and a new row is created with the new fields on an update, with a different primary key).
Being relatively new to Hibernate I'm struggling on how to model this, or even ascertain whether it's possible at all. Well, the deletes are simple enough with a custom @SQLDelete
annotation, but it's the updates that seem tricky. Theoretically this seems like it could be quite a simple thing to do (in meta-code, @SQLUpdate(sql = @SQLDelete + "; " + @SQLInsert)
) but there are obvious complications (asides from the fact that this syntax doesn't exist), partly around the fact that the primary key would need to be nullified in between the two statements and updated in the second, and I'm sure there are other data consistency issues that I haven't considered yet.
Is there a realistic way to manage this, i.e. model updates as delete + insert in Hibernate?
EDIT: Just to clarify, I'm aware that I could get this to work in a brute-force way, by explicitly specifying the insert SQL, and then using the above snippet (with legal references to SQL constants) to do the update. However, I don't really want to do this as I'm happy with the SQL Hibernate chooses to generate by default for the inserts, and writing this out by hand would be very brittle should the class change at all. It's a last-ditch solution to force this through, but it would seem to undermine the point of an ORM mapper somewhat if I have to write the SQL by hand...
DOUBLE EDIT: Even the above presumably won't work as I will need to specify the primary key bind parameter twice in the SQL (not possible with question marks), and I can't persuade Hibernate to go assign me a new one from the SequenceGenerator. So it looks like I'll need a programmatic approach here, rather than a configurational one - unless there's some particularly relevant config elements I've missed.