• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to convert this mysql query to JPA Query?

 
Ranch Hand
Posts: 59
3
  • 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: 27698
196
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: 59
3
  • 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: 79029
375
  • 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: 59
3
  • 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: 27698
196
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: 27698
196
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic