如何在JPA标准中使用projection和where子句?

时间:2017-04-15 04:57:08

标签: java jpa-2.0 criteria-api

我有实体人

@Entity(name = "Person")
public class Person {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
    private Set<Phone> phones=new HashSet<Phone>();

    public Person() {
    }

    public Person(String name) {
        this.name = name;

    }

广告实体电话:

@Entity(name = "Phone")
public class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "person_id", nullable = false)
    private Person person;

    public Phone() {
    }

他们有一对多的关系。 现在我想建立jpa标准这样的查询:

select p.phones from person p join phone ph where p.name = :name;

所以我想从Person实体中提取Set<Phone> phones,其中person的名字是参数。

我写了这个jpa标准查询:

CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<Person> query = builder.createQuery(Person.class);
        Root<Person> root = query.from(Person.class);
        CriteriaQuery<Person> where = query.where(builder.equal(root.get("name"), "Mary Dick"));
        CompoundSelection<Set> projection = builder.construct(Set.class, root.get("phones"));
        where.select(projection); //compile error: The method select(Selection<? extends Person>) in the type CriteriaQuery<Person> is not applicable for the arguments (CompoundSelection<Set>)
    }

但它给出了编译错误:

The method select(Selection<? extends Person>) in the type CriteriaQuery<Person> is not applicable for the arguments (CompoundSelection<Set>)

怎么回事?我需要元模型类吗?

1 个答案:

答案 0 :(得分:0)

CompoundSelection<Y> construct(Class<Y> result, Selection<?>... terms)

仅当查询涉及某些未完全由单个实体类封装的投影时,此方法才有用。如果是这种情况,第一个参数将是自定义POJO类(具有合适的构造函数),其中的字段对应于查询的select子句。

在这种情况下,选择已经是实体类的一部分。因此,您只需选择所需的字段即可。

 CriteriaQuery<Person> query = builder.createQuery(Person.class);
 Root<Person> root = query.from(Person.class);
 query.where(builder.equal(root.get("name"), "Mary Dick"));
 query.select(root.get("phones"));

以上查询将返回人员列表。但是,如果您只是寻找可迭代的手机列表,请尝试稍微不同的查询。

select ph from phone ph join ph.person p where p.name = :name;

及其等效的CriteriaQuery:

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Phone> query = builder.createQuery(Phone.class);
Root<Phone> root = query.from(Phone.class);
Join<Phone, Person> join = root.join(root.get("person"))            
query.where(builder.equal(join.get("name"), "Mary Dick"));