JPA2 Criteria-API:选择... in(从哪里选择)

时间:2012-06-01 16:47:02

标签: spring jpa-2.0 criteria-api specifications

我有以下数据库模型:

A
aId

AB
aId
bId

B
bId
status

在Spring数据规范中,我想在B.status为'X'时返回A的实例。 JPQL代码如下:

select a from A a where a in
     (select ab.id.a from AB ab where ab.id.b.status= :status)

这些是模型类:

@Entity
public class A {
     private Long aId;

     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.a")
     private Set<AB> ab;
}

@Entity
public class B {
     private Long bId;
     private String Status;

     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.b")
     private Set<AB> ab;
}

@Entity
public class AB {
     private ABPK id;
}

public class ABPK {
     @ManyToOne
     @JoinColumn(name="aId")
     private A a;

     @ManyToOne
     @JoinColumn(name="bId")
     private B b;
}

Spring规范中的JPA标准怎么样?

public class ASpecifications {
     public static Specification<A> test(final String status) {
          return new Specification<Party>() {
          @Override
          public Predicate toPredicate(Root<A> a, CriteriaQuery<?> query, CriteriaBuilder cb) {
            return null;
          }
        };
     }
}

4 个答案:

答案 0 :(得分:2)

使用Criteria API返回A实例的规范如下:

public class ASpecifications {
     public static Specification<A> test(final String status) {
          return new Specification<Party>() {
          @Override
          public Predicate toPredicate(Root<A> a, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Subquery<A> sq = query.subquery(A.class);
            Root<AB> ab = sq.from(AB.class);
            sq.select(ab.get(AB_.id).get(ABPK_.a));
            sq.where(cb.equal(ab.get(AB_.id).get(ABPK_.b).get(B_.status), status));

            Predicate p = cb.in(a).value(sq);
            return cb.and(p);
          }
        };
     }
}

答案 1 :(得分:0)

上一篇文章中包含了一些很好的示例,可以准确地解决您在此处要完成的任务:jpa-2-0-criteria-api-subqueries-in-expressions

答案 2 :(得分:0)

我想你想要选择“B实体所提供状态的AB实体中的实体”:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<A> cq = cb.createQuery(A.class);
Root<AB> ab = cq.from(AB.class);
cq.select(ab.get("id").get("a"));
cq.where(cb.equal(ab.get("id").get("b.status"), status));

答案 3 :(得分:0)

选择输入-是一个选项,但使用双重联接可以达到相同的结果。而且更容易制定这样的jpa规范:
SELECT A.ID FROM A LEFT JOIN AB ON A.ID = AB.A_ID LEFT JOIN B ON AB.B_ID = B.ID WHERE B.STATUS = 'STATUS'
方法如下:

public static Specification<A> findB(String input) {
    return new Specification<A>() {
        @Override
        public Predicate toPredicate(Root<A> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
            Join<A,AB> AjoinAB = root.joinList(A_.AB_LIST,JoinType.LEFT);
            Join<AB,B> ABjoinB = AjoinAB.join(AB_.B,JoinType.LEFT);
            return cb.equal(ABjoinB.get(B_.NAME),input);
        }
    };
}

或更短

public static Specification<A> findB(String input) {
    return (Specification<A>) (root, cq, cb) -> {
        Join<A,AB> AjoinAB = root.joinList(A_.AB_LIST,JoinType.LEFT);
        Join<AB,B> ABjoinB = AjoinAB.join(AB_.B,JoinType.LEFT);
        return cb.equal(ABjoinB.get(B_.NAME),input);
    };
}

我知道这个问题已经存在很长时间了,但是当我尝试做同样的事情时我遇到了它。这是我可行的解决方案,希望对您有所帮助。

下面的实体

@Entity
public class A {
    @Id
    private Long id;
    private String name;
    @OneToMany(mappedBy = "a")
    private List<AB> abList;
}
@Entity
public class B {
    @Id
    private Long id;
    private String status;
    @OneToMany(mappedBy = "b")
    private List<AB> abList;
}
@Entity
public class AB {
    @Id
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "a_id")
    private A a;
    @ManyToOne
    @JoinColumn(name = "b_id")
    private B b;
}