#how can i avoid or improve this ?

12 messages · Page 1 of 1 (latest)

unkempt isle
#

First i am using java and postgress as my Database.

i want to create a method that get All the orders and there specific fields:

orderId,
customerId,
List<Appetizer>,
List<Entree>

The first image i am able to do that. But the problem is that i have an N+ 1 issue because of my loop.
Scenario: imagine that you want to get All the orders in a Restaurant. You go to the restaurant and ask for orderId:1 you get which appetizer & entree that come with orderId:1. Then you go back to your table but you still need to get the rest of the orders that you want.(That is the N+1 issue that i am having with my loop.).
I need to find a way to go to the restaurant once to find all the appetizers &
then going to the restaurent for the second time to find all the entrees

how can i do that ?

if you need anymore infomation plz tell

Thank you for your time.

unkempt isle
#

how can i void .improve this ?

#

how can i avoid or improve this ?

hollow folio
#

With Spring Data JPA, you can do a whole lot with auto-generated repository methods and JPQL. What I would do to fetch an order with additional information, is use a custom JPQL query with a join fetch clause. Google it to learn more about it, but essentially it's eager-fetching of an entity's relation.

select Order o
left join fetch o.appetizers apps
left join fetch o.customer customer
where o.id = :id
#

But it's hard to understand what you really want to do because of the poor grammar, so you'll need to explain further

unkempt isle
raven otter
#

Hello, if u are using Spring and Hibernate. Why not using some @OneToMany ?

Something like that :

unkempt isle
#

This is the return that i want to have.But i want to remove the N+1 Issue from the first image

#

I am yet to try the suggestions of @raven otter & @hollow folio . let me say that yesterday was not a good day. But i am going give them a shot

unkempt isle
unkempt isle
#

Both methods unfortunately did not work. Any more suggestions ?