NinjaBoy
NinjaBoy

Reputation: 3755

hibernate - One to One mapping, how to get the associated object using annotation?

I have 2 objects Customer and CustomerAcctSetting. CustomerAcctSetting has a foreign key CUSTOMER_ID. My problem is when I get a Customer object I can't get the associated CustomerAcctSetting and returns only null.

Below are the 2 hibernate object:

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;
    }
}

I did not include any mapping for Customer in the CustomerAcctSetting because I don't have to get Customer from CustomerAcctSetting. I only put CUSTOMER_ID in the CustomerAcctSetting.

Please help. Thanks in advance.

Upvotes: 1

Views: 788

Answers (1)

NinjaBoy
NinjaBoy

Reputation: 3755

I solved this by usinng mappedBy="Customer" in Customer and added the Customer in CustomerAcctSetting and @JoinColumn.

Below are the classes:

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;
    }
}

Upvotes: 2

Related Questions