У меня есть два репозитория, расширяющих JpaRepository, связанных отношением @OneToMany.
Я обнаружил, что при добавлении нового дочернего элемента и вызове метода save () новый дочерний элемент не только сохраняется, но и сохраняется.оба родителя И все вложенные мной дети обновляются, даже если они не грязные.
Ожидается ли это?
Родитель:
@ToString(callSuper=true)
@EqualsAndHashCode(exclude={"id", "version", "exchangeName"}, callSuper=false, doNotUseGetters=true)
@NoArgsConstructor
@Entity()
@Table(name = "Exchanges") // uniqueConstraints = {@UniqueConstraint(columnNames = {"mask", "group"})}
public class ExchangeDbo extends Auditable<String> {
Id @GeneratedValue
@Getter private Long id;
@Version
private long version;
@Getter private String exchangeName;
@Column(unique = true)
@Getter private String exchangeNameLowercase;
@OneToMany(mappedBy = "exchange",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.EAGER) //temp change fetch to lazy
@BatchSize(size=100)
@Getter private List<CurrencyPairDbo> listCurrencyPair = new ArrayList<>();
public ExchangeDbo(@NonNull String name) {
this.exchangeName = name;
this.exchangeNameLowercase = exchangeName.toLowerCase();
}
public CurrencyPairDbo addCurrencyPair(CurrencyPair currencyPair) {
CurrencyPairDbo currencyPairDbo = new CurrencyPairDbo(currencyPair, this);
listCurrencyPair.add( currencyPairDbo );
return currencyPairDbo;
}
}
Ребенок:
@EqualsAndHashCode(exclude={"id","version", "currencyPair"}, doNotUseGetters=true, callSuper=false)
@NoArgsConstructor
@Entity()
@Table(name = "Coinpairs",
indexes = { @Index(columnList = "currencyPair_lowercase", unique = true)
}
)
public class CurrencyPairDbo extends Auditable<String> {
@Id @GeneratedValue
@Getter private Long id;
@Version
private long version;
@Column(unique=true)
@Convert(converter = CurrencyPairConverter.class)
@Getter private CurrencyPair currencyPair;
@Column(unique = true, name="currencyPair_lowercase")
@Getter private String currencyPairNameLowercase;
@ManyToOne(fetch=FetchType.EAGER) //temp change fetch to lazy
@Getter private ExchangeDbo exchange;
@OneToMany(mappedBy = "currencyPair",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.EAGER) //temp change fetch to lazy
@BatchSize(size=100)
@Getter private List<TradeDbo> listTrades = new ArrayList<>();
@Override
public String toString() {
...
}
public CurrencyPairDbo(@NonNull CurrencyPair currencyPair,
@NonNull ExchangeDbo exchangeDbo) {
this.currencyPair = currencyPair;
this.currencyPairNameLowercase = currencyPair.toString().toLowerCase();
this.exchange = exchangeDbo;
}
public void addNewTrade(Trade trade) {
listTrades.add( new TradeDbo(trade, this) );
}
}
Процедура обслуживания:
@Transactional(readOnly=false, rollbackFor=Exception.class)
public DboReturns addNewCurrencyPair(ExchangeDbo exchangeDbo, String currencyPairString) {
CurrencyPairDbo currencyPairDbo = exchangeDbo.addCurrencyPair(new CurrencyPair(currencyPairString));
ExchangeDbo newExchangeDbo = exchangeRepository.save(exchangeDbo);
return new DboReturns(newExchangeDbo, currencyPairDbo, null);
}