我有2个对象Customer
和CustomerAcctSetting
。 CustomerAcctSetting
有一个外键CUSTOMER_ID
。我的问题是当我得到一个Customer
对象时,我无法得到关联的CustomerAcctSetting
并且只返回null。
下面是2个休眠对象:
Customer.java
@Entity
@Table(name = "CUSTOMER")
public class Customer extends BaseDomain{
.
.
.
private CustomerAcctSetting customerAcctSetting;
@Id
@Override
@GeneratedValue(generator = "increment")
@GenericGenerator (name = "increment", strategy = "increment")
@Column (name = "CUSTOMER_ID", unique = true, nullable = false, insertable = false, updatable = false)
public int getId() {
return super.getId();
}
.
.
.
@OneToOne
@JoinColumn(name = "CUSTOMER_ID")
public CustomerAcctSetting getCustomerAcctSetting() {
return customerAcctSetting;
}
public void setCustomerAcctSetting(CustomerAcctSetting customerAcctSetting) {
this.customerAcctSetting = customerAcctSetting;
}
}
CustomerAcctSetting.java
@Entity
@Table(name = "CUSTOMER_ACCT_SETTING")
public class CustomerAcctSetting extends BaseDomain{
private int customerId;
.
.
.
@Id
@Override
@GeneratedValue(generator = "increment")
@GenericGenerator (name = "increment", strategy = "increment")
@Column (name = "CUSTOMER_ACCT_SETTING_ID", unique = true, nullable = false, insertable = false, updatable = false)
public int getId() {
return super.getId();
}
.
.
.
@Column(name = "CUSTOMER_ID")
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
}
我没有在Customer
中添加CustomerAcctSetting
的任何映射,因为我不必从Customer
获取CustomerAcctSetting
。我只将CUSTOMER_ID
放入CustomerAcctSetting
。
请帮忙。提前谢谢。
答案 0 :(得分:2)
我通过在mappedBy="Customer"
中使用Customer
并在Customer
和CustomerAcctSetting
中添加@JoinColumn
来解决此问题。
以下是课程:
Customer.java
@Entity
@Table(name = "CUSTOMER")
public class Customer extends BaseDomain{
.
.
.
private CustomerAcctSetting customerAcctSetting;
@Id
@Override
@GeneratedValue(generator = "increment")
@GenericGenerator (name = "increment", strategy = "increment")
@Column (name = "CUSTOMER_ID", unique = true, nullable = false, insertable = false, updatable = false)
public int getId() {
return super.getId();
}
.
.
.
@OneToOne(mappedBy="customer")
public CustomerAcctSetting getCustomerAcctSetting() {
return customerAcctSetting;
}
public void setCustomerAcctSetting(CustomerAcctSetting customerAcctSetting) {
this.customerAcctSetting = customerAcctSetting;
}
}
CustomerAcctSetting.java
@Entity
@Table(name = "CUSTOMER_ACCT_SETTING")
public class CustomerAcctSetting extends BaseDomain{
private Customer customer;
.
.
.
@OneToOne
@JoinColumn (name="CUSTOMER_ID", insertable=false, updatable=false)
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}