hibernate - 一对一映射,如何使用注释获取关联对象?

时间:2013-01-10 02:28:43

标签: hibernate annotations

我有2个对象CustomerCustomerAcctSettingCustomerAcctSetting有一个外键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

请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我通过在mappedBy="Customer"中使用Customer并在CustomerCustomerAcctSetting中添加@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;
    }
}