如何检索特定列而不是从Entity自动生成所有列或属性

时间:2013-08-27 12:34:05

标签: hibernate jpa jpa-2.0

我有以下实体关系,我希望从Employee类获取员工详细信息。

实体代码段

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

   @ManyToOne
   @JoinColumn(name = "REQUESTED_BY", referencedColumnName = "EMPLOYEE_NUMBER")
   private Employee requestedBy;
   }   

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

   @OneToMany(mappedBy = "requestedBy")
   private Set<Project> employeeRequestedBy;
   }

DAOImpl 代码段

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Project> c = cb.createQuery(Project.class);
Root<Project> project = c.from(Project.class);
project.fetch(Project_.empNumber, JoinType.LEFT);
project.fetch(Project_.requestedBy, JoinType.LEFT);
c.orderBy(cb.desc(project.get("projectId")));
c.select(project);

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

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

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

当我运行我的应用程序时,它会生成以下sql查询。理想情况下,我希望将designationemployee_dept用于requested_by属性,但自动生成的查询也为designation属性生成employee_deptemp_number ,如下面的查询所示。

如何指定仅为designation属性生成employee_deptrequested_by?任何帮助都非常值得赞赏。

SELECT    *
      FROM    (SELECT project0_.project_id AS project1_2_0_,
                      employee1_.emp_number AS emp1_1_1_,
                      employee2_.emp_number AS emp1_1_2_,                            
                      project0_.date_requested AS date7_2_0_,
                      employee1_.employee_dept AS employee5_1_1_,
                      employee1_.designation AS designat6_1_1_,
                      employee1_.employee_name AS employee7_1_1_,
                      employee2_.employee_dept AS employee5_1_2_,
                      employee2_.designation AS designat6_1_2_,
                      employee2_.employee_name AS employee7_1_2_
                      FROM    projects project0_
                      LEFT OUTER JOIN employees employee1_
                      ON project0_.emp_number = employee1_.emp_number
                      LEFT OUTER JOIN employees employee2_
                      ON project0_.requested_by = employee2_.emp_number                            
                      ORDER BY    project0_.project_id DESC)

我的预期查询将是

SELECT    *
      FROM    (SELECT project0_.project_id AS project1_2_0_,
                      employee1_.emp_number AS emp1_1_1_,
                      employee2_.emp_number AS emp1_1_2_,                            
                      project0_.date_requested AS date7_2_0_,
                      employee1_.employee_name AS employee7_1_1_,
                      employee2_.employee_dept AS employee5_1_2_,
                      employee2_.designation AS designat6_1_2_,
                      employee2_.employee_name AS employee7_1_2_
                      FROM    projects project0_
                      LEFT OUTER JOIN employees employee1_
                      ON project0_.emp_number = employee1_.emp_number
                      LEFT OUTER JOIN employees employee2_
                      ON project0_.requested_by = employee2_.emp_number                            
                      ORDER BY    project0_.project_id DESC)

1 个答案:

答案 0 :(得分:1)

您告诉JPA获取Project及其引用的Employees,因此它需要返回完整的实体,否则会破坏上下文。我不知道Hibernate是如何/是否支持惰性基本映射,但这可能是一个选项 - 在Employee中标记您并不总是想要的属性。这将同样影响requestedBy和empNumber关系。

如果您不想要完整的Employee数据,则可能只有查询返回所需的数据。保留查询以获取empNumber但使requestedBy关系变为惰性,并让查询返回Project和所需的Project_.requestedBy数据。 Linda有一个很好的例子,使用multiselect在https://blogs.oracle.com/ldemichiel/entry/the_typing_of_criteria_queries返回数据。