title: Modelling a temporal-style relational table in Hibernate
body: <p>I'm trying to wrap Hibernate around an existing data model, which as expected has its ups and downs.</p>
  
  <p>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).</p>
  
  <p>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 <code>@SQLDelete</code> annotation, but it's the updates that seem tricky.  Theoretically this seems like it <em>could</em> be quite a simple thing to do (in meta-code, <code>@SQLUpdate(sql = @SQLDelete + "; " + @SQLInsert)</code>) 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.</p>
  
  <p>Is there a realistic way to manage this, i.e. model updates as delete + insert in Hibernate?</p>
  
  <p>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...</p>
  
  <p>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), <em>and</em> 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.</p>