Metamodel API and criteria API

431 views Asked by At

I can`t understand one thing from documentation Oracle. I have POJO, for example:

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

And the corresponding metamodel class(witch was generated or written manually):

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;
}

Now I have access to the metamodel fields:

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");

But in documentation i have found the next way to get metamodel:

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

Where it can be useful? After all, in this case I will not have access to the fields of my POJO

0

There are 0 answers