2

My model looks like this:

Customer (Name, Age, ..)

   Project

      Rent

         Fault

(and maybe deeper)

So each Customer has a list of projects. Projects have a list of rents. And rents have a list of faults. Links are bidirectional.

Now I have a Spring MVC application which offers JSP pages and a rest interface.


There should be a service which contains two methods:

Customer readCustomerShallow(long customerId)

This one is for the website and should return a customer with name, age but no projects.

Customer readCustomerDeep(long customerId)

This one is for the rest service and should return a customer including all projects and the projects should contain all rents and the rents should contain all faults.

I want to stick using lazy loading. So readCustomerShallow() is fine already. For readCustomerDeep() I create a deep copy of the customer object with dozer and return that.

Which solution is better?

I know I can use Hibernate.initilize() and walk through all nested objects. But this is much coding affort. Also I know I could make relationships in the model EAGER but then readCustomerShallow() would read all data too.

Thanks a lot.

EDIT: This one does not work:

@Query("select c from Company as c " +
        "join fetch c.departments as d " +
        "join fetch d.persons p " +
        "where c.id = ?1")
Company findOneDeep(long companyId);

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

NoobieNoob
  • 600
  • 1
  • 9
  • 23

0 Answers0