Home > Software engineering >  how to fix org.hibernate.LazyInitializationException and org.hibernate.WrongClassException: with fak
how to fix org.hibernate.LazyInitializationException and org.hibernate.WrongClassException: with fak

Time:05-20

I'm trying to add persistence to a faktor ips project that is based on the intro tutorial from enter image description here enter image description here enter image description here

CodePudding user response:

Try wrapping the method where you load the Angebot with @Transactional so that there is a session existing when lazy loading the assocation. The exception tells you that you are trying to lazy load the association without a transaction (session) with could not initialize proxy - no Session.

I suggest you dig into this article by one of the Hibernate maintainers Vlad Mihalcea where he shows the best way to deal with LazyInitializationExceptions. Eager fetching is definetly not the way to go, in fact it is actually considered a code smell. Apart from the join fetch approach described by Vlad (where you need a JPQL query), you could also use Spring Data JPAs EntityGraph annotation to fetch certain associations eagerly (examples here).

Regarding your second problem: I don't really understand what you mean by "automatically generated annotation". A Java annotation can't be generated automatically or added during runtime, it needs to be placed before compiling your code. You could try omitting the targetEntity attributes in your association annotations, I have never needed this attribute so far and I don't think that you need it as the Javadoc states that Optional only if the collection property is defined using Java generics.. Could you also show your code for the HausratGrunddeckung and HausratZusatzdekung class?

  • Related