benstpierre
benstpierre

Reputation: 33581

Using Hibernate/JPA without relationships and avoiding multiple DB calls

It seems to me that when you use relationships in Hibernate/JPA that using relationships like OneToMany can have a performance boost on reads because only one database call would need to be run to get a parent entity and all children entities. I want to avoid using relationships and just map foreign key columns as normal columns due to the nature of my application.

One problem is that when I actually want to handle relationships I need to do code like this...

ParentEntity pe => someDao.findBySomething("some param"); //db round trip List<ChildEntity
childEntities = someDao.findChildren(pe); //db round trip

It seems like there is some way to do things a big more manually like I want while avoiding the extra round trips. Any ideas?

Upvotes: 3

Views: 2282

Answers (2)

Bozho
Bozho

Reputation: 597106

You are also free to used @NamedQueries and HQL/JPA-QL instead of your mappings. It would be easier to write. For example:

childEntities = someDao.findByQuery("Child.findChildrenOfParent", parentId);

Where Parent.findChildren is

SELECT c FROM Child c WHERE c.parentId=:parentId

Upvotes: 2

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

If you want to avoid relationships then you lose a significant advantage of JPA. However, you can still do what you want with Native SQL which is supported by Hibernate.

Upvotes: 3

Related Questions