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

Retrieve data from database columns that have a many to many relationship. JPQL /JPA

 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone!




As you can see, my class ordemservico has a list of objects form layouts and the class layouts has a list ordemservico .

On my ordemservico data base I have a reference (a number) to the list of layouts, which are in another table. My question is: how can I retrieve this list of layouts objects that are associated with that number. I need that list to put on a jtable and the person can edit
 
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
Jrcastro Ribeiro wrote:On my ordemservico data base I have a reference (a number) to the list of layouts, which are in another table. My question is: how can I retrieve this list of layouts objects that are associated with that number. I need that list to put on a jtable and the person can edit

And what have you tried yourself so far? Did youalready have a look at the Common Queries section of the Java Persistence WikiBook?
 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that:





With the id from ordemservico I can get the layouts that related to that ordemservico, if im think correct...



this is the first solution that comes to mind, there is a better?



 
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
Jrcastro Ribeiro wrote:this is the first solution that comes to mind, there is a better?

Looks good! But does your solution give the desired results? Because according to the Common Queries section this query is used for a OneToMany relationship and you have a ManyToMany relationship, so you might need to use another query...
 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Jrcastro Ribeiro wrote:this is the first solution that comes to mind, there is a better?

Looks good! But does your solution give the desired results? Because according to the Common Queries section this query is used for a OneToMany relationship and you have a ManyToMany relationship, so you might need to use another query...


Yes!

Returned to list that I was expecting.

But my query was only in one direction : ordemservico > layouts

I just needed to find the Layouts of a especific "ordemservico" .

Example: "ordemservico" n°50, has 5 layouts, I just needed the names of these 5 layouts.

Maybe work because of that? dont know if what I'm saying makes sense...
 
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
Jrcastro Ribeiro wrote:Yes!

Returned to list that I was expecting.

Well done!

From a SQL perspective it looked like the appropriate query to write for your use case. And if it returns the expected results, that's awesome. You can't ask for more

Have a cow for solving your own question and coming back to share this solution. Other ranchers might benefit from it!

Kind regards,
Roel
 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Jrcastro Ribeiro wrote:Yes!

Returned to list that I was expecting.

Well done!

From a SQL perspective it looked like the appropriate query to write for your use case. And if it returns the expected results, that's awesome. You can't ask for more

Have a cow for solving your own question and coming back to share this solution. Other ranchers might benefit from it!

Kind regards,
Roel



Wow!!

Thank you sir!

What I can say is that this forum has helped me a lot to learn several thing I had difficulty
 
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
Jrcastro Ribeiro wrote:What I can say is that this forum has helped me a lot to learn several thing I had difficulty

Glad to hear you have learned a lot already

That's actually the purpose of this website: it's not about just providing a ready-to-use solution to an issue/problem; but all about explaining the cause(s) of the issue/problem and providing some hints/clues/tips/suggestions to solve it, so you have a good understanding of everything required to solve the issue/problem yourself. CodeRanch is all about: give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic