我有两个扩展JpaRepository的存储库,它们与@OneToMany关系相关联。
我发现当我添加一个新子节点并调用save()方法时,新子节点不仅会被保存,而且父节点和我附加的所有子节点都会更新,即使它们不是脏的。 / p>
这是预期的吗?
父:
public virtual ObjectResult<long?> GetMaximumIDsCount2(Nullable<int> appID, Nullable<int> isBurnCenter, Nullable<int> burnCenterID)
{
var appIDParameter = appID.HasValue ?
new ObjectParameter("AppID", appID) :
new ObjectParameter("AppID", typeof(int));
var isBurnCenterParameter = isBurnCenter.HasValue ?
new ObjectParameter("isBurnCenter", isBurnCenter) :
new ObjectParameter("isBurnCenter", typeof(int));
var burnCenterIDParameter = burnCenterID.HasValue ?
new ObjectParameter("BurnCenterID", burnCenterID) :
new ObjectParameter("BurnCenterID", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<Nullable<long>>("GetMaximumIDsCount @AppID, @isBurnCenter, @BurnCenterID", appIDParameter, isBurnCenterParameter, burnCenterIDParameter);
}
子:
@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) );
}