These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: createCriteria executes the select query multiple times
PostPosted: Thu Dec 08, 2011 1:38 pm 
Newbie

Joined: Sun Oct 30, 2011 5:43 am
Posts: 13
i use createCriteria with uniqueResult to select an object by id as follows:

1- Service:


Code:
@Service
        @Transactional
        public class MyService {
   
        public Employee getEmployeeById(long employeeId) {
            return employeeDao.getEmployeeById(employeeId);     
           }
   
       }


2- DAO:

Code:
@Repository
      public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {

      public Employee getEmployeeById(long employeeId) {
        return (Employee) super.getById(Employee.class, employeeId);
         }

      }


3- AbstractDao:


Code:
@Repository
    public class AbstractDao {

    @Autowired
    private SessionFactory sessionFactory;

    public Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
       }

     public Object getById(Class clazz, long idValue) {
        return getCurrentSession().createCriteria(clazz)
                .add(Restrictions.idEq(idValue)).uniqueResult();
       }


    }


4- Entity:


Code:
@Entity
    @Table(name = "employee")
    public class Employee implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "employee_id", unique = true, nullable = false)
    @Basic(fetch = FetchType.EAGER)
    private long id;

    @Column(name = "first_name", length = 100, nullable = false)
    private String firstName;

    @Column(name = "last_name", length = 100, nullable = false)
    private String lastName;

    @Column(name = "email", length = 155, nullable = false)
    private String email;

    @Column(name = "password", nullable = false)
    private String password;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "fk_department_id", nullable = true)
    private Department department;

    @ManyToMany(fetch = FetchType.LAZY)
    @Fetch(FetchMode.SELECT)
    @JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
    private Set<Role> roles = new HashSet<Role>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.employee")
    @Cascade(value = { CascadeType.ALL })
    @Fetch(FetchMode.SELECT)
    private Set<EmployeeGroup> employeeGroups = new HashSet<EmployeeGroup>(0);

    }


PROBLEM: when calling the service method that gets an employee by id, while showing SQL, i noticed that the select employee query is getting executed 3 times as follows:


Code:
Hibernate:
        select
            this_.employee_id as employee1_2_0_,
            this_.fk_department_id as fk13_2_0_,
            this_.email as email2_0_,
            this_.first_name as first5_2_0_,
            this_.last_name as last8_2_0_,
            this_.password as password2_0_
        from
            employee this_
        where
            this_.employee_id = ?
    Hibernate:
        select
            this_.employee_id as employee1_2_0_,
            this_.fk_department_id as fk13_2_0_,
            this_.email as email2_0_,
            this_.first_name as first5_2_0_,
            this_.last_name as last8_2_0_,
            this_.password as password2_0_
        from
            employee this_
        where
            this_.employee_id = ?
    Hibernate:
        select
            this_.employee_id as employee1_2_0_,
            this_.fk_department_id as fk13_2_0_,
            this_.email as email2_0_,
            this_.first_name as first5_2_0_,
            this_.last_name as last8_2_0_,
            this_.password as password2_0_
        from
            employee this_
        where
            this_.employee_id = ?


any ideas why i get such behavior and how to adjust it ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.