【发布时间】:2015-03-07 00:06:16
【问题描述】:
我有两个名为 Customer 和他的 Biiling Address 的实体。关系是一对一的。每个客户都有一个账单地址。 我想在保留客户时自动保留帐单地址。 客户 id 是客户实体的主键,也是地址实体的主键和外键。
//parent table
public class CustomerDTO implements Serializable {
@Id
@GeneratedValue
@Column(name = "customer_id")
private Integer id;
@OneToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL )
@PrimaryKeyJoinColumn(name="customer_id")
BillingAddressDTO billingAddressDTO;
//child table
public class BillingAddressDTO implements Serializable {
@Id
@Column(name="customer_id")
private Integer id;
这是我用来持久化实体的代码
customerDTO = new CustomerDTO();
customerDTO.setFirstName(firstName);
billingAddressDTO = new BillingAddressDTO();
billingAddressDTO.setBillingAddress(address1);
customerDTO.setBillingAddressDTO(billingAddressDTO);
//persisting customer entity
customerDAO.persist(customerDTO);
我收到以下异常
Caused by: org.hibernate.id.IdentifierGenerationException: ids for this
class must be manually assigned before calling save():
我想将相同的客户 ID 分配给地址表,所以我不想手动分配它。感谢您的宝贵时间。
【问题讨论】: