How to resolve LazyInitializationException in Spring Data JPA?

后端 未结 6 1559

I have to classes that have a one-to-many-relation. When I try to access the lazily loaded collection I get the LazyInitializationException. I searching the web

6条回答
  •  旧巷少年郎
    2020-12-05 11:22

    You should enable Spring transaction manager by adding @EnableTransactionManagement annotation to your context configuration class.

    Since both services have @Transactional annotation and default value property of it is TxType.Required, current transaction will be shared among the services, provided that transaction manager is on. Thus a session should be available, and you won't be getting LazyInitializationException.

提交回复
热议问题