title: How to save parent and child in one call (Hibernate, JPA)
body: <p>I know, that this question has been asked many times, but my case is different (e.g. <a href="https://stackoverflow.com/questions/53647672/how-to-save-parent-and-child-in-one-shot-jpa-hibernate">How to save parent and child in one shot (JPA &amp; Hibernate)</a>)</p>
  <p>I have DTOs:</p>
  <pre class="lang-java prettyprint-override"><code>@Data
  public class ExchangeRatesTableDto {
      private ExchangeRatesTableType table;
      private String no;
      private LocalDate tradingDate;
      private LocalDate effectiveDate;
      private List&lt;ExchangeRateDto&gt; rates;
  }
  
  @Data
  public class ExchangeRateDto {
      private String currency;
      private String code;
      private Double mid;
      private Double bid;
      private Double ask;
  }
  </code></pre>
  <p>And Entities:</p>
  <pre class="lang-java prettyprint-override"><code>@Data
  @Entity
  @Table(schema = DatabaseNames.SCHEMA_NBP)
  public class ExchangeRatesTable {
  
      @Id
      @GeneratedValue(generator = &quot;UUID&quot;)
      @GenericGenerator(name = &quot;UUID&quot;, strategy = &quot;org.hibernate.id.UUIDGenerator&quot;)
      private UUID id;
  
      @Enumerated(EnumType.ORDINAL)
      private ExchangeRatesTableType tableType;
  
      private String no;
      private LocalDate tradingDate;
      private LocalDate effectiveDate;
  
      @OneToMany(mappedBy = &quot;exchangeRatesTable&quot;, cascade = CascadeType.ALL, orphanRemoval = true)
      private Set&lt;ExchangeRate&gt; exchangeRates;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Data
  @Entity
  @Table(schema = DatabaseNames.SCHEMA_NBP)
  public class ExchangeRate {
  
      @Id
      @GeneratedValue(generator = &quot;UUID&quot;)
      @GenericGenerator(name = &quot;UUID&quot;, strategy = &quot;org.hibernate.id.UUIDGenerator&quot;)
      private UUID id;
  
      @ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
      @JoinColumn(foreignKey = @ForeignKey(name = &quot;FK_exchange_rates_table&quot;))
      private ExchangeRatesTable exchangeRatesTable;
  
      private String currency;
      private String code;
      private Double mid;
      private Double bid;
      private Double ask;
  }
  </code></pre>
  <p>I have two repositories too:</p>
  <pre class="lang-java prettyprint-override"><code>public interface ExchangeRatesTableRepository extends JpaRepository&lt;ExchangeRatesTable, UUID&gt; {
  }
  
  public interface ExchangeRateRepository extends JpaRepository&lt;ExchangeRate, UUID&gt; {
  }
  </code></pre>
  <p>And in controller I have this code:</p>
  <pre class="lang-java prettyprint-override"><code>ModelMapper modelMapper = new ModelMapper();
  ExchangeRatesTableDto table = exchangeRatesService.getTable(exchangeRatesTableType);
  ExchangeRatesTable exchangeRatesTable = modelMapper.map(table, ExchangeRatesTable.class);
  exchangeRatesTableRepository.save(exchangeRatesTable);
  </code></pre>
  <p><code>exchangeRatesService</code> returns <code>ExchangeRatesTableDto</code> with childs.
  ModelMapper returns:
  <a href="https://i.stack.imgur.com/blP3X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/blP3X.png" alt="ExchangeRatesTable object" /></a></p>
  <p>But <code>save</code> method is not working:</p>
  <pre><code>Hibernate: /* insert model.ExchangeRatesTable */ insert into nbp.exchange_rates_table (effective_date, no, table_type, trading_date, id) values (?, ?, ?, ?, ?)
  Hibernate: /* insert model.ExchangeRate */ insert into nbp.exchange_rate (ask, bid, code, currency, exchange_rates_table_id, mid, id) values (?, ?, ?, ?, ?, ?, ?)
  2021-04-14 14:27:42.889  WARN 11404 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23502
  2021-04-14 14:27:42.889 ERROR 11404 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column &quot;exchange_rates_table_id&quot; of relation &quot;exchange_rate&quot; violates not-null constraint
    Szczegóły: Failing row contains (f2a461e5-80ce-4837-aab9-a87919c978f8, null, null, BRL, real (Brazylia), 0.6658, null).
  could not execute statement; SQL [n/a]; constraint [exchange_rates_table_id&quot; of relation &quot;exchange_rate]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  
  </code></pre>