带有uniqueResult的createCriteria多次执行select查询

时间:2011-12-08 17:00:28

标签: hibernate java-ee criteria criteria-api hibernate-criteria

我使用带有uniqueResult的createCriteria来按ID选择对象,如下所示:

1- 服务:

    @Service
    @Transactional
    public class MyService {

    public Employee getEmployeeById(long employeeId) {
        return employeeDao.getEmployeeById(employeeId);      
       }

    }

2- DAO:

  @Repository
  public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {

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

  }

3- AbstractDao:

@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
@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);

}

问题:在调用通过id获取员工的服务方法时,在显示SQL时,我注意到select员工查询执行了3次,如下所示:

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 = ?

任何想法为什么我会得到这样的行为以及如何调整它?

1 个答案:

答案 0 :(得分:0)

为什么首先使用createcriteria?如果您已经知道实体的id,则对Session.load(Class,Id)的简单调用要简单得多。 (http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#load%28java.lang.Class,%20java.io.Serializable%29

你能创建一个简单的测试方法并发布它的源以及它的运行日志吗?