0 Replies Latest reply on Jul 9, 2008 3:39 AM by hipa

    Pessimistic lock

    hipa

      I have the table from which I want to get unique values for some entities.

      public class UniqueGenerator
      {
       @Id
       @Column(...)
       private String id;
      
       @Column(...)
       private Long currentValue;
       ...
      }
      


      Then in data access layer I have to write this code to retrieve next value:
      ...
      gen = entityManager.find(UniqueGenerator.class, MY_GENERATOR_ID);
      entityManager.lock(gen, LockModeType.WRITE);
      entityManager.refresh(gen);
      
      Long currVal = gen.getCurrentValue();
      ...
      


      Hibernate translate it to these 3 queries:
      1. SELECT .. FROM UNIQUE_GENERATORS WHERE ...
      2. SELECT GENERATOR_ID FROM UNIQUE_GENERATORS WHERE ...FOR UPDATE
      3. SELECT .. FROM UNIQUE_GENERATORS WHERE ...
      


      Is there any way to make Hibernate doing only one query:
      SELECT ... FROM UNIQUE_GENERATORS WHERE ... FOR UPDATE
      

      ?