0

I have project where all the models relations (either 1:m or 1:1) fetch types are defined as lazy. Now consider I have a model called M that has three relation a,b and c and all are 1:m. Now I have three services: Service-A Service-B Service-C

If Service-A return model M then its relation a must be populated although other b,c contains proxy model. If Service-B return model M then its relation b must be populated although other a,c contains proxy model. same as for Service-C

Can anyone know how to accomplished that ?

Another scenario here if there is only one Service and user provide as parameter to load relation a or b or c then how to accomplish that ?

2
  • 1
    You could look at using the Entity Graph functionality introduced in JPA 2.1 (Hibernate versions 4.3+ I think). See here for further info. thoughts-on-java.org/jpa-21-entity-graph-part-1-named-entity Commented Dec 15, 2016 at 13:29
  • Good solution thanks thats the answer i was waiting. Commented Dec 17, 2016 at 8:59

1 Answer 1

1

Simplest way is to populate them inside transaction:

in ServiceA entity.getCollectionB().size()

in ServiceB entity.getCollectionC().size()

See other ways: http://www.thoughts-on-java.org/5-ways-to-initialize-lazy-relations-and-when-to-use-them/

EDIT: Lazy loading of one to one relation won't work with entity graph either. With or without it you will need bytecode instrumentation. Optimizing columns read is not so important as optimizing row reads. See: http://www.thoughts-on-java.org/jpa-21-entity-graph-part-1-named-entity/#comment-219

Sign up to request clarification or add additional context in comments.

3 Comments

Alan hay gives me the best solution.
This created [n+1] hibernate issue as well.
In the link are 5 approaches, suitability differs on use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.