2

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.

Krish
  • 1,804
  • 7
  • 37
  • 65
  • So your question is "is it necessary to create the User_ class to use "User_.email"" ????!!! Well obviously. If the class doesn't exist then how can Java load the class? – Neil Stockton Nov 18 '15 at 07:25
  • Then how below code (which is in Oracle documentation) works ?CriteriaQuery cq = cb.createQuery(Pet.class); Metamodel m = em.getMetamodel(); EntityType Pet_ = m.entity(Pet.class); Root pet = cq.from(Pet.class); cq.where(cb.equal(pet.get(Pet_.name), "Fido")); Also if staticMetamodel class is required then what is the need of below 2 lines: Metamodel m = em.getMetamodel(); EntityType Pet_ = m.entity(Pet.class); Please explain – Krish Nov 18 '15 at 08:54
  • Pet_ is part of the GENERATED STATIC METAMODEL. Read documentation http://www.datanucleus.org/products/accessplatform_4_2/jpa/jpql_criteria.html#metamodel – Neil Stockton Nov 18 '15 at 09:00
  • I agree User_ is part of StaticMetamodel. My concern is, by creating User_ with StaticMetamodel annotation we can write as below: CriteriaQuery criteria = builder.createQuery(User.class); Root userRoot = criteria.from(User.class); criteria.select(userRoot); criteria.where(builder.equal(userRoot.get(User_.email), user.getEmail())); But in the documentation http://docs.oracle.com/javaee/6/tutorial/doc/gkjbq.html , Pet_ is obtained using 2 lines of code 1. MetaModel-> 2. EntityType What is the need of getting Metamodel class from EntityType as above ? – Krish Nov 18 '15 at 09:27
  • Who said this "Oracle tutorial" you refer to is correct? It isn't. Look at the DOCUMENTATION of the implementation you are using. Look at the JPA spec. They are the definitive references – Neil Stockton Nov 18 '15 at 09:34

1 Answers1

6

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.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • Its my bad to follow oracle doc. Thanks for correcting. – Krish Nov 18 '15 at 12:35
  • Well, its Oracle's bad for publishing such incorrect info (they are *supposed* to be the people who promote this technology). I've raised an issue against them https://java.net/jira/browse/JPA_SPEC-121 – Neil Stockton Nov 18 '15 at 13:31