title: Saving list items in Entity Spring JPA
body: <p>I want to save a list of items in <strong>Configuration</strong> entity. But when the model's <strong>addItem()</strong> function gets called the <strong>this.getItems()</strong> is going to be <strong>NULL</strong>. I dont understand why... What is the best approach to save an object nd it's items in the same request?</p>
  <p><em>Configuration.java</em></p>
  <pre><code>@Entity
  @Getter
  @Setter
  @SuperBuilder
  @NoArgsConstructor
  public class Configuration extends BaseEntity{
  
      @ManyToOne
      private CarModel carModel;
  
      @ManyToOne
      private Country country;
  
      @OneToMany(fetch = FetchType.LAZY)
      private List&lt;EquipmentItem&gt; items = new ArrayList&lt;&gt;();
  
      public void addItem(EquipmentItem equipmentItem){
  
          this.getItems().add(equipmentItem);
      }
  }
  </code></pre>
  <p><em>ConfigurationServiceImpl.java</em></p>
  <pre><code>@Component
  public class ConfigurationServiceImpl implements ConfigurationService{
  
      @Autowired
      private ConfigurationRepository configurationRepository;
  
      @Autowired
      private EquipmentItemRepository equipmentItemRepository;
  
      @Override
      public ConfigurationDTO save(ConfigurationDTO configurationDTO) {
          Configuration configuration = Configuration.builder()
                  .name(configurationDTO.getName())
                  .build();
  
          for (EquipmentItemDTO item: configurationDTO.getItems()) {
              configuration.addItem(equipmentItemRepository.findById(item.getId())
                      .orElseThrow(EntityNotFoundException::new));
          }
  
          configurationRepository.save(configuration);
          return DTOConverter.convertConfiguration(configuration);
      }
  }
  </code></pre>