Reputation: 7484
If I have an entity A with one-to-many relationship with entity B and I have to fetch A along with associated Bs, I can either use EntityManager's find method OR I can write JPQL query using JOIN.
Which approach is better in terms of efficiency and minimum DB calls?
Upvotes: 1
Views: 196
Reputation: 1691
I'm assuming that the EntityManager.find(class, primary key, ...) invocation will load all associated B's thanks to proper usage of @JoinTable and @OneToMany annotations thus the find and the JPQL JOIN produce the same result in a single invocation.
If that is true then my experience with Hibernate and JPA (2.0/2.1) is that there is no difference especially if you use a secondary cache which I do. Do whatever is convenient.
Having said that the only way to know for sure would be to perform the timing yourself. There are differences not only between different JPA implementations such as Hibernate and EclipseLnk but between different versions of the same system.
Additionally your JPQL performance will vary depending upon if you have the query precompiled (I always do this) or it is a dynamic Criteria JPQL. Additionally various Secondary Caches effect performance greatly.
Upvotes: 1