// select c from Customer c where c.firstName = 'Vincent'
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Customer30> query = builder.createQuery(Customer30.class);
Root<Customer30> c = query.from(Customer30.class);
In this test case, the Eclipse_3.6 IDE gives a compliation error at the last line where it says the field Customer30.firstName is not visible.
In the Class Customer30, the field firstName is a private instance variable. This is in a book by a highly respected Java EE architect and given as an
example. I know I could change the field to public or some such, but I don't understand the syntax. Customer30 is a class name, not an instance name.
firstName is a private field in the class and is not static. What is intended here? Is this something that I don't have set right in Eclipse so that it recognizes it?
Or, is it just wrong? If so, what is the correct syntax to accomplish this?
The following code snippet shows how to obtain the Pet entity’s metamodel class by first obtaining a metamodel instance by using EntityManager.getMetamodel and then calling entity on the metamodel instance:
At this point, it is probably easiest to use the second way Christian mentioned. To use the Metamodel classes, you first have to generate the Metamodel, which is not (yet) painless, and provider-specific to boot.
So, you basically use a provider-specific jar and a build script to generate your metamodel. I've noticed that most tutorials don't actually go in depth on how to generate the Metamodel.
When you download code from a book support site and follow the directions in the book, you expect the code to work, especially the test cases.
I realized that I could get the result without using the meta-model, but felt that since it was there and "supposed to work", that perhaps my installation or configuration was incorrect. Thus the frustration.
I appreciate your insight and help!
Where do I "like" you ;-)
Joined: Mar 13, 2008
Was just editing a reply...
Dieter is right, both codes will produce the same SQL query in the end, and the second is quicker to use assuming you don't have generated the metamodel.
Also I never had to use the JPA Metamodel, I only know it exists, and I didn't try the code samples from Beginning Java EE 6 after reading it.
For an example with metamodel generation, please refer to Criteria API with EclipseLink from the same author.