1 Reply Latest reply on Aug 13, 2008 6:25 PM by lbernardelli

    With FlushMode AUTO, SELECT query with entityManager updates database?

    terryb
      I have a situation where database gets updated when I run a select query created with entityManager.createQuery(...). This happens when entity being queries is dirty, and seam FlushModeType is AUTO.

      Scenario:
      I have a product edit page based on EntityHome with FlushModeType=AUTO. When user modifies data, eg product code, the entityHome.update() methods is envoked. In update methods I run a lookup query to check if product code already exists; and only call super.update() if it does not exist.

      My attempt with lookup query is to read fresh product data from database and compare it with the value typed by the user. However the lookup select query, first executes sql update statement with product code entered by the user, and then do a select on it...not what I intend to do.

      I believe it is due to flushmode being auto, and entity being dirty; causing hibernate to first update it before querying.

      I will appreciate any comments on best approach to cater for these kinda situation?


      Scenario:

      I have a product edit page based on EntityHome with FlushModeType=AUTO. When user modifies data, eg product code, the entityHome.update() methods is envoked. In update methods I run following query to check if productCode already exists; and only call super.update() if it does not exist.

      entityManager.createQuery("Select p from Product p where p.code=:code")

      My attempt is to read fresh product data from database and compare it with the value typed by teh use. However above Select query, first executes sql Update statement with product code entered by the user, and then do a select on it.

      @name("productHome")
      public class productHome extends EntityHome<E> implements Serializable {

          @In(value = "productService", required = false, create = true)
          private ProductService productService;

          ...

          @Override
          public String update() {
         
              String status = "";
             
              if (productService.findProductByCode(this.getInstance().getCode()) != null) {
                  status = Constant.Database.Record.Status.RECORD_EXIST;
                  ...
              } else {
                  status = super.update();
                  ...
              }
              return status;
          }
          ...

      }



      @Name("productService")
      public class ProductService {

          @In(value = "entityManager", required = false, create = true)
          protected EntityManager em;

          ...

          private List<Product> findProductByCode(String code) {
             
              Query query = em.createQuery(select p from Product p where p.code=:code);
              query.setParameter("code", code);
              List<Product> products = query.getResultList();
              return products;
          }
      }