Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Criteria API and Join

 
L Foster
Ranch Hand
Posts: 185
10
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
A few weeks back, I was attempting to learn more about JOINs against JPA criteria queries. I like Criteria because it potentially eliminates all SQL text. But I ran into some problems with JOIN, because I simply could not figure out what to do with a Join object once I found out how to create one. The examples I saw made no sense. One was joining an object type to other instances of itself, and I don't think there was any mention of the join field. I did eventually get something like a join to happen, with the correct output, but it did not use anything like a Join object, so I fear that at any kind of scale it would not be efficient.

Can anyone point me to material on Java Persistence API, that explicitly shows Joins between two different tables (two mapped objects, or possible joins involving unmapped tables), and does these joins on object fields (which are of course represented in Object space by SQL Columns)?

I have looked at the tutorials, and at one book I found. I think during the days of Hibernate Criteria, this subject was much easier to understand. I think JPA has done a better job of encapsulating the SQL, however.

Thanks in advance.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the JPQL section of the Java Persistence API WikiBook might be (very) helpful.
 
L Foster
Ranch Hand
Posts: 185
10
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @Roel, the JPQL section did not help, but right next to it, was the Criteria section. https://en.wikibooks.org/wiki/Java_Persistence/Criteria . Perhaps that's what you meant? That might actually help, because it does move between two tables. One thing that is giving me some pause, however, is that there are still strings being used ("address"). That may be a sign this one is not up-to-date.

Either way, thanks for your help. The whole Wikibooks thing might pay off in spades.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Foster wrote:Perhaps that's what you meant?

No, I did meant the JPQL section. Simply because I rarely use the JPA Criteria API

L Foster wrote:That might actually help, because it does move between two tables. One thing that is giving me some pause, however, is that there are still strings being used ("address"). That may be a sign this one is not up-to-date.

I assume you want to use the metamodel instead of the strings. I think it's probably up-to-date but the examples were written initially with strings (because the metamodel was introduced later on), and they didn't update all the examples but provided a JPA Criteria API example in the metamodel section which should provide you with enough information to adjust the other examples accordingly. But I'm definitely not a JPA Criteria API expert so I could be wrong on this one.

L Foster wrote:Either way, thanks for your help. The whole Wikibooks thing might pay off in spades.

Definitely! Whenever I am in doubt about a JPA related issue, I always have a look at this resource first and very often I find the answer to my question.
 
L Foster
Ranch Hand
Posts: 185
10
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It did work. I wound up using the metamodel (the trailing-underscore autogenerated classes) as a way to resolve columns. Otherwise, my code looked very similar.

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic