title: hibernate criteria query select for update order by
body: <p>I have the following code that uses Hibernate Criteria API. </p>
  
  <pre><code>    List&lt;?&gt; results = getCurrentSession()
              .createCriteria(PersonEvent.class)
              .add(Restrictions.eq(STATUS, EventStatus.NEW))
              .addOrder( Order.asc("sequence") )
             .setLockMode(LockMode.PESSIMISTIC_WRITE)
              .setMaxResults(maxResults).list();
  </code></pre>
  
  <p>It generates the following sql</p>
  
  <pre><code>select
          * 
      from
          ( /* criteria query */ select
              this_.SEQUENCE as SEQUENCE0_0_,
              this_1_.CREATED_UTC as CREATED3_0_0_,
              this_1_.UPDATED_UTC as UPDATED5_0_0_,
              this_1_.STATUS as STATUS0_0_,
              this_.PERSONID as PERSONID1_0_ 
          from
              ACT.PERSON_EVENT this_ 
          inner join
              ACT.EVENT this_1_ 
                  on this_.SEQUENCE=this_1_.SEQUENCE 
          where
              this_1_.STATUS='NEW' 
              order by this_1_.SEQUENCE) 
      where
          rownum &lt;= 50 for update
  </code></pre>
  
  <p>Which results in </p>
  
  <pre><code>ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc.
  02014. 00000 -  "cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc."
  </code></pre>
  
  <p>I have two tables ACT.EVENT (parent) and ACT.PERSON_EVENT (child) modeled using hibernate table per subclass inheritance with one-one relationship. So entity class PersonEvent extends abstract entity class Event.</p>
  
  <pre><code>ACT.EVENT
  -SEQUENCE (PK)
  -STATUS
  -CREATED_UTC
  -UPDATED_UTC
  
  ACT.PERSON_EVENT
  -SEQUENCE (PK, FK)
  -PERSONID
  </code></pre>
  
  <p>How can I write this same query with order by using Criteria API.</p>