This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Meaning of this syntax - from Beginning EE 6 java book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Meaning of this syntax - from Beginning EE 6 java book" Watch "Meaning of this syntax - from Beginning EE 6 java book" New topic
Author

Meaning of this syntax - from Beginning EE 6 java book

John Wooten
Greenhorn

Joined: Aug 29, 2008
Posts: 7
// 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);
query.select(c).where(builder.equal(c.get(Customer30.firstName), "Vincent"));


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?
Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
Should be if the goal is to use the Criteria metamodel API (new in JPA 2.0).

It's equivalent to
I'll check my own copy of Antonio's book

Christian Gossart
John Wooten
Greenhorn

Joined: Aug 29, 2008
Posts: 7
When I change Customer30.firstName to Customer30_.firstName I get the error Customer30_ cannot be resolved to a variable.

I guess I've missed something. If I put an underscore after a classname, then it is referring to "metadata"?

Anyway, this did not work as you can see. BTW, this was the downloaded code from the book site.
John Wooten
Greenhorn

Joined: Aug 29, 2008
Posts: 7
Do I have to add a part of this into the code?
The following code snippet shows how to obtain the Pet entity’s metamodel class by calling Root<T>.getModel:

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

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:

EntityManager em = ...;
Metamodel m = em.getMetamodel();
EntityType<Pet> Pet_ = m.entity(Pet.class);

OR, since annotations are there in the code, does the Customer30_ class get created by Eclipse?
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
John Wooten
Greenhorn

Joined: Aug 29, 2008
Posts: 7
Thanks very much!

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

Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
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.

And feel free to post an errata at Beginning Java EE 6 or at Antonio's web site.

Good luck on your JPA learning,
 
GeeCON Prague 2014
 
subject: Meaning of this syntax - from Beginning EE 6 java book