To initialize or not initialize JPA relationship mappings?

后端 未结 4 1268
死守一世寂寞
死守一世寂寞 2021-01-01 12:58

In one to many JPA associations is it considered a best practice to initialize relationships to empty collections? For example.

@Entity
public class Order {          


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 13:22

    I would rather prefer an utility like this:

    public static  void forEach(Collection values, Consumer consumer) {
      if (values != null) values.stream().forEach(consumer);
    }
    

    and use it in code like:

    Utils.forEach(entity.getItems(), item -> {
        // deal with item
    });
    

提交回复
热议问题