This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Meaning of this syntax - from Beginning EE 6 java book

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Wooten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Wooten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Whatever you say buddy! And I believe this tiny ad too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!