如何使用JPA规范在非主键列上创建查询

huangapple go评论150阅读模式
英文:

How to create query using JPA specification on non primary column

问题

以下是翻译好的部分:

  1. 我有以下查询其中两个表在非主键列上进行连接表格通过一个共同的列进行连接
  2. 在实体中我没有提供UserUserDetails之间的连接并且我也不想提供
  3. 如何在JPA规范中实现相同的查询
英文:

I have the below query, where two tables are joining on non primary column. The table is joining on one common column.

In entity, I have not provided any joining between User and UserDetails, and I don't want to provide either

  1. SELECT * from User user, UserDetails ud
  2. WHERE user.user_key = :pUserKey // passed in parameter
  3. user.common_key = ud.common_key

User

  1. @Entity
  2. @Table(name = "USER")
  3. @Data
  4. public class User implements java.io.Serializable {
  5. private static final long serialVersionUID = 1L;
  6. @Id
  7. @Column(name = "user_key", nullable = false)
  8. @JsonIgnore
  9. private Long userKey;
  10. @Column(name = "common_key ", nullable = false)
  11. @JsonIgnore
  12. private Long commonKey;
  13. }

UserDetails

  1. @Entity
  2. @Table(name = "USER_DETAILS")
  3. @Data
  4. public class UserDetails implements java.io.Serializable {
  5. private static final long serialVersionUID = 1L;
  6. @Id
  7. @Column(name = "user_details_key", nullable = false)
  8. @JsonIgnore
  9. private Long userDetailsKey;
  10. @Column(name = "common_key ", nullable = false)
  11. @JsonIgnore
  12. private Long commonKey;
  13. }

How to achieve the same query in JPA specification

答案1

得分: 1

我已按预期获得了解决方案。我使用query.from()创建了新的root

以下是完整的解决方案:

  1. public static Specification<User> userAndDetails(Long userKey) {
  2. return (Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
  3. Root<UserDetails> userDetailsRoot = query.from(UserDetails.class);
  4. Predicate userAndDetailsJoin = builder.equal(
  5. root.get(User_.commonKey),
  6. userDetailsRoot.get(UserDetails_.commonKey));
  7. Predicate userKeyPredicate = builder.equal(root.get(User_.userKey),
  8. userKey);
  9. return builder.and(userAndDetailsJoin, userKeyPredicate);
  10. };
  11. }
英文:

I have got the solution as expected. I have created new root using query.from()

Below is the complete solution

  1. public static Specification&lt;User&gt; userAndDetails(Long userKey) {
  2. return (Root&lt;User&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder builder) -&gt; {
  3. Root&lt;UserDetails&gt; userDetailsRoot = query.from(UserDetails.class);
  4. Predicate userAndDetailsJoin = builder.equal(
  5. root.get(User_.commonKey),
  6. userDetailsRoot.get(UserDetails_.commonKey));
  7. Predicate userKeyPredicate = builder.equal(root.get(User_.userKey),
  8. userKey);
  9. return builder.and(userAndDetailsJoin, userKeyPredicate);
  10. };
  11. }

huangapple
  • 本文由 发表于 2020年8月19日 13:49:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63480756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定