Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to convert this mysql query to JPA Query?

 
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to convert this mysql query to JPA Query?
I tried following an example and I ended up with the query below.


SELECT * FROM informativos
    INNER JOIN informativos_usuarios
    ON informativos_usuarios.user_id = user.id
    AND
    informativos_usuarios.informativo_id = i.id;






Additional information:






 
Saloon Keeper
Posts: 25470
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, one of the big advantages of JPA over raw SQL/JDBC is that you don't code JOIN statement in JPQL.

JPA is based on Entity classes, so if you define informativos  as having a relation such as @OneToMany to informativos_usuarios, then a simple

is all the JPQL you need. Depending on whether you've defined the relation as LAZY_FETCH or EAGER_FETCH, the informativos_usuario(s) linked to the informativo in question  will be immediately fetched as well.
 
Castiel Snow
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Well, one of the big advantages of JPA over raw SQL/JDBC is that you don't code JOIN statement in JPQL.

JPA is based on Entity classes, so if you define informativos  as having a relation such as @OneToMany to informativos_usuarios, then a simple

is all the JPQL you need. Depending on whether you've defined the relation as LAZY_FETCH or EAGER_FETCH, the informativos_usuario(s) linked to the informativo in question  will be immediately fetched as well.



Hi, Thanks for the reply.
I understand however, I need to get a list of informativos ,"i", if i contains user u;
 
Marshal
Posts: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't add new information to old posts which have been replied to; it is likely that the new text will not be noticed.
 
Castiel Snow
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please don't add new information to old posts which have been replied to; it is likely that the new text will not be noticed.



Sorry. I thought would be considered spam. Wont happen again.
 
Tim Holloway
Saloon Keeper
Posts: 25470
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your list of Users in informativos is not annotated as a related Entity with @OneToMany or @ManyToMany.

And while you can query primarily on informativos, you would find it simpler for User to back-reference to informativos and select from User.

 
Tim Holloway
Saloon Keeper
Posts: 25470
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I'm blind. So many annotations stacked up I didn't see what they were annotating!

You do need a reverse @ManyToMany back from User, though. As I said, it's much simpler to retrieve a User and read its list of informativos.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic