A: 

I have no experience in Hibernate, so I don't know if you are free to change the DB at your will or if Hibernate requires a specific DB structure you cannot change.

If you can make changes then you can use this workaround in MSSQL tu emulate the ANSI behaviour :

drop the unique index/constraint

define a calc field like this:

alter table MyTable Add MyCalcField as 
case when MyUniqueField is NULL 
      then cast(Myprimarykey as MyUniqueFieldType) 
      else MyUniqueField end

add the unique constraint on this new field you created.

Naturally this applies if MyUniqueField is not the primary key! :)

You can find more details in this article at databasejournal.com

Giacomo Degli Esposti
+1  A: 

You could try flushing the hibernate session in between the two saves. This may force Hibernate to perform the first update before the second insert.

Also, when you say that hibernate is inserting NULL with the insert, do you mean every column is NULL, or just the ID column?

skaffman