我知道,这个问题已经被问过很多次了,但我的情况不同(例如,如何一次性保存父母和孩子(jpa&hibernate))
我有DTO:
@Data
public class ExchangeRatesTableDto {
private ExchangeRatesTableType table;
private String no;
private LocalDate tradingDate;
private LocalDate effectiveDate;
private List<ExchangeRateDto> rates;
}
@Data
public class ExchangeRateDto {
private String currency;
private String code;
private Double mid;
private Double bid;
private Double ask;
}
和实体:
@Data
@Entity
@Table(schema = DatabaseNames.SCHEMA_NBP)
public class ExchangeRatesTable {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Enumerated(EnumType.ORDINAL)
private ExchangeRatesTableType tableType;
private String no;
private LocalDate tradingDate;
private LocalDate effectiveDate;
@OneToMany(mappedBy = "exchangeRatesTable", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ExchangeRate> exchangeRates;
}
@Data
@Entity
@Table(schema = DatabaseNames.SCHEMA_NBP)
public class ExchangeRate {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
@JoinColumn(foreignKey = @ForeignKey(name = "FK_exchange_rates_table"))
private ExchangeRatesTable exchangeRatesTable;
private String currency;
private String code;
private Double mid;
private Double bid;
private Double ask;
}
我也有两个存储库:
public interface ExchangeRatesTableRepository extends JpaRepository<ExchangeRatesTable, UUID> {
}
public interface ExchangeRateRepository extends JpaRepository<ExchangeRate, UUID> {
}
在控制器中,我有一个代码:
ModelMapper modelMapper = new ModelMapper();
ExchangeRatesTableDto table = exchangeRatesService.getTable(exchangeRatesTableType);
ExchangeRatesTable exchangeRatesTable = modelMapper.map(table, ExchangeRatesTable.class);
exchangeRatesTableRepository.save(exchangeRatesTable);
``` `exchangeRatesService` 退货 `ExchangeRatesTableDto` 和孩子们在一起。modelmapper返回:
![](https://i.stack.imgur.com/blP3X.png)
但是 `save` 方法无效:
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 "exchange_rates_table_id" of relation "exchange_rate" 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" of relation "exchange_rate]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
1条答案
按热度按时间z18hc3ub1#
您必须手动设置每个
ExchangeRate
对象ExchangeRatesTable
对象,就像你一样ExchangeRatesTable
setter方法。然后它就不会是空的,也会保存在数据库中。在代码中可以迭代
ExchangeRateList
并设置ExchangeRatesTable
所有元素的目标。