Metamodel API和标准API

时间:2017-08-31 12:22:45

标签: java hibernate-criteria criteria-api metamodel

我无法从文档Oracle中了解一件事。我有POJO,例如:

package com.example;
...
@Entity
public class Pet {
    @Id
    protected Long id;
    protected String name;
    protected String color;
    @ManyToOne
    protected Set<Person> owners;
    ...
}

相应的元模型类(女巫是手动生成或编写的):

package com.example;
...
@Static Metamodel(Pet.class)
public class Pet_ {

    public static volatile SingularAttribute<Pet, Long> id;
    public static volatile SingularAttribute<Pet, String> name;
    public static volatile SingularAttribute<Pet, String> color;
    public static volatile SetAttribute<Pet, Person> owners;
}

现在我可以访问元模型字段:

EntityManager em = ...
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Pet> c = qb.createQuery(Pet.class);
Root<Pet> p = c.from(Pet.class);
Predicate condition = qb.equal(p.get(Pet_.name), "someName");

但是在文档中我找到了下一个获取元模型的方法:

EntityManager em = ...;
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Pet.class);
Root<Pet> pet = cq.from(Pet.class);
EntityType<Pet> Pet_ = pet.getModel();

哪里有用?毕竟,在这种情况下,我将无法访问我的POJO

的字段

0 个答案:

没有答案