具有uniqueResult的createCriteria多次执行选择查询

问题描述 投票:2回答:1

我使用具有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时,我注意到选择员工查询正在执行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 = ?

关于为什么我会出现这种行为以及如何进行调整的任何想法?

hibernate java-ee criteria hibernate-criteria
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.