Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is JPA's n+1 problem related to @OneToMany or to @ManyToOne or both?

Sure there are many articles about n+1 JPA’s problem and also Q&As here, some of them discuss the problem from @OneToMany perspective, others from @ManyToOne is the problem relevant for both?

@OneToMany: when query entity from which have a field collection annotated @OneToMany

@ManyToOne: when query entity from which have a field annotated @ManyToOne

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For both even for @OneToOne when it’s lazy loaded.

The problem occurs if you load a list of entities where the entities have relationships to one or more entities.

So the first query is getting the list and then for every entity in the list JPA will load the relationships with an additional query.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading