tags:

views:

150

answers:

2

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?

+1  A: 

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.

Vincent Ramdhanie
The fact that I can write queries that understand inheritance and outputs optimized sql for more than one database is very useful. I have found that only using relationships when you always want to load the entire object graph from the DB at once makes sense. Not to mention by using Hibernate great libraries like Warp-Persist make my life a lot easier by removing boilerplate code.
Benju
+1  A: 

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
Bozho