我无法与@EmbeddedId
在最后一个实体中使用的持久三个级别的实体进行级联合并。我有以下三个实体:
1. Voucher
2. Rate
3. RatePerChannelOverwrite
由Voucher
服务获取。然后我想Rate
在凭证中添加新的。然后我想RatePerChannelOverwrite
在Rate
. 所以两者都Rate
将RatePerChannelOverwrite
它们@Id
作为NULL。这是有效的。
但是后来我尝试RatePerChannelOverwrite
分成两个班级并拥有:@Embeddable RatePerChannelOverwriteId
. 这个设置失败了。被Rate
引用的对象RatePerChannelOverwriteId
永远不会填充其新 ID。
@Entity
public class Voucher {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@OneToMany(mappedBy = "voucher", fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL)
private Set<Rate> rates = new HashSet<Rate>();
}
@Entity
public class Rate {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voucher_id", referencedColumnName = "id")
private Voucher voucher;
@Column(name = "rate", precision = 2, scale = 9, columnDefinition = "decimal")
@NumberFormat(pattern = "#0.00")
private double rate;
@OneToMany(mappedBy = "rate", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
private Set<RatePerChannelOverwrite> ratePerChannelOverwrites;
}
@Entity
public class RatePerChannelOverwrite {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@ManyToOne
@JoinColumn(name = "rate_id", referencedColumnName = "id")
private Rate rate;
@Column(name = "channel_id")
private Integer channelId;
@Column(name = "rate", precision = 2, scale = 9, columnDefinition = "decimal")
@NumberFormat(pattern = "#0.00")
private double rate;
}
当我做这样的事情时,这很好用:
Voucher voucher = service.getVoucher(123456);
Rate rate = new Rate(); //NOTE ID IS NULL HERE
rate.setVoucher(voucher);
voucher.getRates().add(rate);
RatePerChannelOverwrite channelRate = new RatePerChannelOverwrite();
channelRate.setRate(rate);
rate.getRatePerChannelOverwrites.add(channelRate);
voucher = voucher.merge();
但是,当我将最后一个实体分成 2 个单独的类时,id
inrate
始终RatePerChannelOverwriteId
为 NULL。不知何故,这里的级联失败了。
注意:第三个实体具有与 一样@EmbeddedId
的复合键@ManyToOne
,这就是问题出现的地方。
@Embeddable
public class RatePerChannelOverwriteId {
@ManyToOne
@JoinColumn(name = "rate_id", referencedColumnName = "id")
private Rate rate;
@Column(name = "channel_id")
private Integer channelId;
}
@Entity
public class RatePerChannelOverwrite {
@EmbeddedId
private RatePerChannelOverwriteId id;
@Column(name = "rate", precision = 2, scale = 9, columnDefinition = "decimal")
@NumberFormat(pattern = "#0.00")
private double rate;
}
这样做是否正确?
Voucher voucher = service.getVoucher(123456);
Rate rate = new Rate();
rate.setVoucher(voucher);
voucher.getRates().add(rate);
RatePerChannelOverwriteId channelRateId = new RatePerChannelOverwriteId();
channelRateId.setRate(rate);
RatePerChannelOverwrite channelRate = new RatePerChannelOverwrite();
channelRate.setId(channelRateId);
rate.getRatePerChannelOverwrites.add(channelRate);
voucher = voucher.merge();
我得到的实际异常是来自 'org.hibernate.type.descriptor.AbstractTypeDescriptor.extractHashCode(T value)' 的 NullPointer,其中 value 为 NULL,因为id
inRate
仍然为 NULL(尽管 SQL 跟踪显示 INSERT 是在 上执行的Rate
)。