user883769
user883769

Reputation: 18

Hibernate Mapping issue

I've 3 tables: A, B and C. A holds a one-to- many relation with B and B holds a one-to-one relation with C. When I do a session.save(objA), a row will be created in A, many rows will be created in B referring to the id of A and one row should be created in C for each entry in B referring to the id of B.

Now the problem is, A and B are getting populated as expected, but in C, rows are getting populated but the column containing id of B is populated with null value.

Is it the problem with the mapping given in hbm.xml?

B.hbm.xml

    <one-to-one name="propertyC" class="com.model.C" 
       cascade="all" fetch="join">
    </one-to-one>    

C.hbm.xml

    <many-to-one name="propertyB" class="com.model.B" 
       column="B_ID"  unique ="true" fetch="join" update="true" insert="true" cascade="all">
    </many-to-one>

B.java

    class B{
    private Long id;
    private C propertyC;
    }

C.java

    class C{
    private Long id;
    private Long bId;
    private B propertyB;
    }

Upvotes: 1

Views: 155

Answers (1)

GaetanZ
GaetanZ

Reputation: 3069

If you have a oneToOne relation beetween B and C why this is defined like a manyToOne in the C mapping (and not a OneToOne) ?

Furthermore, if you have a bidirectional relationship, you have to define the mappedBy element of the relationship:

@Entity
private static class B {
    @Id
    private Long id;

    @OneToOne
    private C propertyC;
}


@Entity
class C {

    @Id
    private Long id;
    private Long bId;

    @OneToOne (mappedBy="propertyC")
    private B propertyB;
}

Upvotes: 1

Related Questions