OneToOne加入了专栏问题

时间:2015-11-22 19:02:20

标签: hibernate jpa spring-data-jpa

我遇到了@OneToOne单向映射的问题。父级PK未保存在子连接列上。例如customer_payment的invoice_master_id_pay应与CustomerInvoiceMaster的customer_invoice_master一起插入。

这是映射。

@Entity
@Table(name = "customer_invoice_master")
public class CustomerInvoiceMaster {

@Id
private int invoiceMasterId;

// invoice_master_id_pay should be populated with 
// CustomerInvoiceMaster customer_invoice_master PK. This is not
// not happening. Row is created with zero value 
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "invoice_master_id_pay")
private CustomerPayment customerPayment;

}

@Entity
@Table(name="customer_payment")
public class CustomerPayment{
    @Id
    private int customerPaymentId; 

...
}

2 个答案:

答案 0 :(得分:0)

您展示的代码不会将CustomerInvoiceMaster PK存储在invoice_master_id_pay上,相反,这种一对一的映射会将customerPaymentId存储在invoice_master_id_pay

父母是使用连接列引用孩子的。

答案 1 :(得分:0)

CustomerPayment cp=new CustomerPayment();
// assign properties to cp;
CustomerInvoiceMaster cim=new CustomerInvoiceMaster();
// assign properties to cti (including cp);
...
session.save(cim);   // would save both cp,cim;  

如果您希望session.save(cp);同时保存cp,请使用OneToOne双向关联。