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

Jrcastro Ribeiro

Greenhorn

Posts: 29

1

posted 1 year ago

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

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 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 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...

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