I have an entity that spans two tables, primary_table and secondary_table. I've annotated it properly with the @SecondaryTable annotation.
It just so happens that in my database, primary_table has more records in it than secondary_table.
Another way to think of this is that the primary_table does not just store instances of my entity, but instances of other objects as well.
At any rate, if I create a new instance of my entity, and set its @Id to a value that exists in the primary table, I am pleased to see that Hibernate recognizes that oh, hey, there's a primary_table record in there already, so it does an INSERT on the secondary_table, as it should, and an UPDATE on the primary_table. Everything is fine.
My problem is actually...well, Hibernate is the only JPA provider that does this. The other two (of the Big Three) both attempt blind INSERTs into the primary table. When that @Id value exists in the primary_table, then of course the transaction rolls back with a primary key violation.
My question is: is this a known optimization taken by Hibernate--i.e. this is a problem that the authors foresaw and solved? Or is this just happy chance? The specification as far as I can tell is somewhat quiet on this point. I'd be curious to get any pointers or answers related to this subject.
Thanks very much, Laird
|