Is it good practice to initialize fields inside a JPA entity getter?

后端 未结 4 1481
南笙
南笙 2020-12-17 15:13

In POJO Java beans such code can be beneficial, especially with collections:

class POJO {
    private Collection<         


        
4条回答
  •  心在旅途
    2020-12-17 15:47

    I would strongly discourage lazy initialization for properties in an ORM entity.

    We had a serious issue when doing lazy initialization of an entity property using Hibernate, so I would strongly discourage it. The problem we saw was manifest by a save taking place when we were issuing a search request. This is because when the object was loaded the property was null, but when the getter was called it would return the lazy initialized object so hibernate (rightly) considered the object dirty and would save it before issuing the search.

提交回复
热议问题