【问题标题】:Returning a Join of Entity A and Entity B and store in Entity C in Spring JPA without JQL/HQL在没有 JQL/HQL 的 Spring JPA 中返回实体 A 和实体 B 的连接并存储在实体 C 中
【发布时间】:2018-03-09 06:45:19
【问题描述】:

我有以下实体:

@Entity
public class Employee {
       @Column
       private String employeeName;

       @Column
       private Integer employeeId;

       @OnetoMany
       private Set<Account> accounts;
}

@Entity
public class Account {
       @Column
       private String bankName;

       @Column
       private Integer accountNumber; 
}


public class EmployeeAccountRecords {
       private String employeeName;
       private Integer accountNumber;
}

我想获取 Employee 和 Account 连接的分页结果,并将值存储在 EmployeeAccountRecords 类的对象中。此外,我还想将排序/过滤器传递给实现此功能的存储库。

这是否可以在不使用 JPQL/HQL 编写查询的情况下完成,可能正在使用 JPA 规范等,任何其他组件?

我正在使用带有 Hibernate 的 Spring Data JPA。

谢谢。

PS:结果对象仅使用连接值中结果集的子集。

【问题讨论】:

    标签: java database spring spring-data spring-data-jpa


    【解决方案1】:

    不使用 JPQL/HQL,我看到的另外两个选项是 Spring Data JPA Specifications 和 QueryDsl。

    根据链接 Support projections on query methods that take a dynamic query type Specification or Querydsl PredicateAdd support for QueryDSL projections in JPA repositories 他们还不支持投影。

    但他们确实有链接显示如何在 querydsl http://stackoverflow.com/questions/18300465/spring-data-and-querydsl-to-fetch-subset-of-columns-using-bean-constructor-proje 中扩展存储库并支持这些功能,另一个是 specification-with-projection,可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2013-11-27
      • 2021-11-19
      相关资源
      最近更新 更多