I have tried to access the MetaModel entity attributes/variables using the code below:
CriteriaQuery<User> criteria = builder.createQuery(User.class);
Metamodel m = entityManager.getMetamodel();
EntityType<User> User_ = m.entity(User.class);
Root<User> userRoot = criteria.from(User.class);
criteria.where(builder.equal(userRoot.get(User_.email)), user.getEmail());
but email cannot be resolved or is not a field. Is it mandatory to create StaticMetaModel class for User class. i.e., "User_" ???
If YES, please see the link http://docs.oracle.com/javaee/6/tutorial/doc/gkjbq.html here, you can find the below code:
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
Metamodel m = em.getMetamodel();
EntityType<Pet> Pet_ = m.entity(Pet.class);
Root<Pet> pet = cq.from(Pet.class);
cq.where(cb.equal(pet.get(Pet_.name), "Fido"));
Please help me here.
That Oracle documentation you refer to is utterly wrong. The STATIC metamodel is generated by an annotation processor. It is not obtained via the JPA Metamodel class.
The STATIC (canonical) metamodel class with "_" is not an EntityType
object.
See the JPA spec section 6.2.1.1 and documentation such as this one.