title: Hibernate merge where clause
body: <p>I have looked around for this answer, but I could not find anything that answered my question.</p>
  
  <p>I am working on an application that uses Hibernate and it uses <code>session.merge(object)</code> to do the update or insert of the object. The insert works fine, but the update will fail due to a unique constraint on database fields (A, B, C) if multiple records exist with the same values for (A, B). The Hibernate model only has fields (A, B) defined as the id, it does not have (A, B, C) because when selecting or updating records only the record with a null value of C is wanted to be returned (C is a termination date where null means active and non-null means not active).</p>
  
  <p>In the Hibernate model file (Table.hbm.xml), it has a where clause defined as follows:</p>
  
  <pre><code>&lt;class name="..." table="..." lazy="true" batch-size="10" where="C is null"&gt;
  </code></pre>
  
  <p>That gets inserted when doing selects, but when doing the merge statement, the update statement does not have this as part of the where clause. The generated update is something like:</p>
  
  <pre><code>update table
  set ...
  where A=?
  and B=?
  </code></pre>
  
  <p>That is fine, but what I would like is to also have the where clause from the Hibernate model file (the <code>C is null</code> clause) to be added to the where clause like it is for select statements.</p>
  
  <p>Does anyone know what I can do to get that added to the update statement?</p>
  
  <p>Thank you for your help.</p>