Question

I am using Dozer to map from one to second object, but Iam geting error:

 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ... , could not initialize proxy - no Session

When I use before mapping field.size() for all collections it works, but its really annoying. Is there some option to load hibernate object with initialized all collections eagrly?|

Was it helpful?

Solution

This is not dozer's problem. You are accessing yor data in a wrong way. In your hibernate entity, you have some lazy reference (to collection, another entity,...), but you are converting it to some bean outside hibernate session (your entity is in so called 'detached' state).

Solution for this problem depends on your situation. You can:

  • exclude given attribute (if you don't need it), so it will be not accessed by dozer
  • move dozer conversion into block of code, which is inside hibernate's session
  • re-attach your entity if you still have reference to the current session

The mapped field size may work even with lazy collections. The number of elements may be known, but the content not.

EDIT: more solutions:

  • mark referenced collection as eager (not lazy), but this may slow down other queries and increase memory consumption (not recommended)
  • initialize referenced lazy collection using Hibernate.initialize()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top