Should persistent classes initialize instance variable collections

后端 未结 4 1723
慢半拍i
慢半拍i 2021-01-04 08:43

In my Hibernate classes should instance collections be initialized

public class Basket {
    private List items = new ArrayList();

    ...getters and setter         


        
4条回答
  •  半阙折子戏
    2021-01-04 09:14

    You should initialize it. Even if Hibernate where to initialize it later even if it had no content (which I'm not sure it's always the case), you should initialize it to be sure that it's always coherently not-null.

    In the provided example you can see that Cat has no explicit no-argument constructor, so they have to initialize the set at declaration. Most likely when you see it left uninitialized there was also an explicit no-arg constructor that initialized it later.

提交回复
热议问题