java - 使用@Embeddable Annotation 休眠一对多/多对一
问题描述
当我试图保存客户对象时,异常指出它不能将 NULL 插入 ()...
我相信异常是由于使用了@Embeddable Annotation 引起的
但是,当我明确设置 customer_id 的值时,它可以正常工作,但该值不会作为外键保存在 Customerphone 表中。
@Entity
@Table(name = "CUSTOMER")
public class Customer implements java.io.Serializable {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "Customer")
public Set<CustomerPhone> getCustomerPhones() {
return this.CustomerPhones;
}
}
@Entity
@Table(name="CUSTOMER_PHONE")
public class CustomerPhone implements java.io.Serializable {
private CustomerPhoneId id;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="customerId", column=@Column(name="CUSTOMER_ID", nullable=false, precision=22, scale=0) ),
@AttributeOverride(name="phoneTypeCd", column=@Column(name="PHONE_TYPE_CD", nullable=false, length=5) ) } )
public CustomerPhoneId getId() {
return this.id;
}
public void setId(CustomerPhoneId id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="CUSTOMER_ID", nullable=false, insertable=false, updatable=false)
public Customer getCustomer() {
return this.stpCustomer;
}
}
@Embeddable
public class CustomerPhoneId implements java.io.Serializable {
private BigDecimal customerId;
@Column(name="CUSTOMER_ID", nullable=false, precision=22, scale=0)
public BigDecimal getCustomerId() {
return this.customerId;
}
}
解决方案
您的 ID 没有 Generation 架构,例如@GeneratedValue(strategy=GenerationType.AUTO)
.
在这些情况下,您需要手动设置 ID,在您的情况下,您需要customerId
在持久化之前手动设置。
推荐阅读
- python - TensorDataset 上的 PyTorch 转换
- elixir - Phoenix 检查模型(例如用户)是否相等
- python - Raspberry Pi 3 使用 720p USB 摄像头提高 FPS
- swift - 编码偏移量弃用
- extjs - 如何在 Ext JS 4 中将按钮置于其容器的中心?
- r - 根据对内的条件过滤数据框
- c# - 错误:通过 API 使用文本框中的值在 Nextcloud 上创建用户/组
- c# - 如何通过 AuthorizationHandlerContext 获取策略名称
- jpa - Spring数据存储库如何查询包含子字符串的String的ElementCollection
- wordpress - 如何使用 ACF 和 cron 作业自动更改帖子类别