1

我有 2 个对象CustomerCustomerAcctSetting. 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我不必CustomerCustomerAcctSetting. 我只放CUSTOMER_IDCustomerAcctSetting.

请帮忙。提前致谢。

4

1 回答 1

2

我通过使用mappedBy="Customer"in解决了这个问题Customer并添加了CustomerinCustomerAcctSetting@JoinColumn.

以下是课程:

客户.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;
    }
}
于 2013-01-10T03:23:37.490 回答