Hibernate查询执行的次数超过预期

时间:2013-08-25 11:43:22

标签: hibernate jpa jpa-2.0

我有下面提到的实体关系。当我运行我的应用程序时,我可以看到Hibernate生成的以下查询执行了13次(1 + 12),其中12是我在前端应用程序中显示的行数。理想情况下它应该只执行一次因为我不确定为什么它会被执行12次。如果我将要在前端应用程序中显示的行数更改为20,则查询将执行21次。

如何解决此问题?

Hibernate自动生成查询

select employee0_.EMPLOYEE_NUMBER as EMP1_2_0_, employee0_.DEPARTMENT_CODE as  
DEPARTMENT3_2_0_, employee0_.EMPLOYEE_DEPT as EMPLOYEE4_2_0_, employee0_.DESIGNATION
as DESIGNAT5_2_0_, employee0_.EMPLOYEE_NAME as EMPLOYEE6_2_0_  from EMPLOYEES 
employee0_ where employee0_.EMPLOYEE_NUMBER=?

实体类

   @Entity
   public class Project
   @ManyToOne
   @JoinColumn(name = "EMPLOYEE_NUMBER", referencedColumnName = "EMPLOYEE_NUMBER")
   private Employee requester;

   @ManyToOne
   @JoinColumn(name = "APPROVER", referencedColumnName = "EMPLOYEE_NUMBER")
   private Employee approver;
   }

   @Entity
   public class Employee {
   @OneToMany(mappedBy = "requester")
   private Set<Project> requestedProjects;

   @OneToMany(mappedBy = "approver")
   private Set<Project> approvedProjects;
   }

JSF UI来源

<p:dataTable id="dataTable" var="proj" lazy="true"
    value="#{projMB.lazyModel}" styleClass="projTableStyle"
    paginator="true" paginatorPosition="bottom" rows="12"
    rowKey="#{proj.projectId}">
    <f:facet name="header">         

    <p:column id="employeeNo" headerText="Employee Name" width="16">
    <h:outputText value="#{proj.empNumber.employeeName}" />
    </p:column>
        .....
        .....

DAOImpl类检索记录的位置

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Project> c = cb.createQuery(Project.class);
Root<Project> emp = c.from(Project.class);
c.orderBy(cb.desc(emp.get("projectId")));
c.select(emp);

CriteriaQuery<Long> countQ = cb.createQuery(Long.class);
Root<Project> empCount = countQ.from(Project.class);
countQ.select(cb.count(empCount));

List<Predicate> criteria = new ArrayList<Predicate>();
if (projectId != null) {
ParameterExpression<String> pexp = cb.parameter(String.class,
 "projectId");
Predicate predicate = cb.equal(emp.get(Project_.projectId), pexp);
criteria.add(predicate);
            }

TypedQuery<Project> q =  entityManager.createQuery(c);

控制器类

private List<Project> project = new ArrayList<Project>();
project = myservice.getDAOValues();
....

该表格显示以下内容

Employee No, Employee Name, Project Id, Project Start Date 

修改2

@ManyToOne
@JoinColumn(name = "REQUESTOR", referencedColumnName = "EMPLOYEE_NUMBER", 
insertable = false, updatable = false)
private RequesterDepartment requesterDepartment;

1 个答案:

答案 0 :(得分:1)

您希望在单个查询中加载项目及其各自的请求者或批准者。这是使用获取连接完成的。在JPQL中:

select p from Project p
left join fetch p.requester
where ...

在Criteria中,有一个等价的结构:

Root<Project> project = c.from(Project.class);
project.fetch(Project_.requester, JoinType.LEFT);

请注意,我将emp重命名为projectemp几乎是可接受的Employee路径名称。将它用于Project路径非常令人困惑。