title: Using "SELECT FOR UPDATE OF" with Hibernate 4 & Postgres
body: <p>I'm using Postgres 9.3.5 and recently updated the hibernate from 3.2 to 4.3.11.</p>
  
  <p>As a result I can't run "<strong>SELECT... FOR UPDATE OF</strong>" queries,
  and simply 'select.. for update' is not enough in my case since it returns </p>
  
  <blockquote>
    <p>could not extract ResultSet. Reason: ERROR: FOR UPDATE cannot be applied to the nullable side of an outer join</p>
  </blockquote>
  
  <p>The criteria I'm trying to use looks like this: </p>
  
  <pre><code> Criteria criteria = session.createCriteria(objectType).add(Restrictions.eq("name", objectName).ignoreCase());
  </code></pre>
  
  <p>I'm using the following locking:</p>
  
  <p>in 3.2:     criteria.setLockMode(LockMode.UPGRADE); </p>
  
  <p>in 4.3.11: criteria.setLockMode(LockMode.PESSIMISTIC_WRITE);</p>
  
  <p>I have an hierarchy of hibernate (&amp; DB) objects which cause the hibernate perform several joins while constructing the above query. 
  the 'objectType' is a <strong>joined-subclass</strong> of the main class</p>
  
  <pre><code>&lt;class name="BaseObject" table="BASE_OBJECTS"&gt;
  </code></pre>
  
  <p>While using hibernate 3.2 the final query (taken from Postgres logs) ended with: "<strong>for update of this_2_</strong>"<br>
  (when <strong>this_2_</strong> is the alias given by hibernate to the main table (BaseObject) mapped in hbm.xml file)</p>
  
  <p>After upgrading to 4.3.1.1 the same query returns the above mentioned exception.
  which means the final query performed as <strong>for update</strong> (without the name of the table on which to perform the lock)</p>
  
  <p>After an extensive look of the web I could find only that the "for update of" in hibernate with Postgres is not supported any more?</p>
  
  <p>[<a href="https://hibernate.atlassian.net/browse/HHH-5654][2]" rel="nofollow">https://hibernate.atlassian.net/browse/HHH-5654][2]</a></p>
  
  <p>It seems very unlikely since it's quite an important sql feature and a big degradation in usage. </p>
  
  <p>Am I missing something here? </p>
  
  <p><strong>02.09.15:</strong></p>
  
  <p>I'll try to clarify myself:</p>
  
  <p>using an example given in the hibernate documentation
  at </p>
  
  <blockquote>
    <p><a href="https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/inheritance.html" rel="nofollow">https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/inheritance.html</a></p>
  </blockquote>
  
  <pre><code>class name="Payment" table="PAYMENT"&gt;
  &lt;id name="id" type="long" column="PAYMENT_ID"&gt;
      &lt;generator class="native"/&gt;
  &lt;/id&gt;
  &lt;property name="amount" column="AMOUNT"/&gt;
  ...
  &lt;joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT"&gt;
      &lt;key column="PAYMENT_ID"/&gt;
      &lt;property name="creditCardType" column="CCTYPE"/&gt;
      ...
  &lt;/joined-subclass&gt;
  &lt;joined-subclass name="CashPayment" table="CASH_PAYMENT"&gt;
      &lt;key column="PAYMENT_ID"/&gt;
      ...
  &lt;/joined-subclass&gt;
  &lt;joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT"&gt;
      &lt;key column="PAYMENT_ID"/&gt;
      ...
  &lt;/joined-subclass&gt;
  </code></pre>
  
  <p></p>
  
  <p>If I want to perform something like:
  <strong>select p from Payment p where id=1</strong></p>
  
  <p>Hibernate will perform an outer join (on the key) on all tables .</p>
  
  <p>Adding a lock (<em>.setLockMode(LockMode.PESSIMISTIC_WRITE)</em>) will lock the lines on the four tables (as 'For update'),
  instead of only on table "Payments" ('for update of p') - which did happen in hibernate 3.2</p>
  
  <p>So what We have, is that Something which was supplied earlier by hibernate, is not working any more, using their own mapping examples?</p>
  
  <p>Thanks in advance
  Marina</p>