Hibernate unable to FETCH child entitiy

637 Views Asked by At

I have two entities namely Student and Address. The parent Entity being Student and the Child entity being Address. Considering that each student can have only one Address and each Address can belong to only one student,and declaring the Address reference only in Student makes it a unidirectional one-to-one mapping.

Both Student and Address data are persisted via REST API(POST Mapping) separately. Here the parent entity Student is being persisted separately and the child entity is also being persisted separately via 2 different end points.

But while doing a GET operation on Student I would like to include Address that belongs to the Student in the resource that is exposed to the consumer.

    public class Student{

private String studentId; //pk
private String firstName;
private String lastName;
private Address address;

 //getters and setters
  }




 public class Address{

private String addressId; //pk
private String streetAddress;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String country;
private String zipCode;
//getters and setters
}

STUDENT TABLE

    CREATE TABLE STUDENT (
    STDNT_ID VARCHAR(20) PRIMARY KEY,
    FIRST_NAME VARCHAR(30) NOT NULL,
    LAST_NAME VARCHAR(30) NOT NULL
);

ADDRESS TABLE

    CREATE TABLE ADDRESS (
    A_ID VARCHAR(20) PRIMARY KEY,
    STREET_ADDRESS VARCHAR(20), 
    ADDR1 VARCHAR(20) NOT NULL,
    ADDR2 VARCHAR(20) NOT NULL,
    CITY VARCHAR(20)NOT NULL,
    STATE VARCHAR(20)NOT NULL,
    COUNTRY VARCHAR(20) NOT NULL,
    ZIPCODE INTEGER(5)NOT NULL,
    STDNT_ID VARCHAR(20) UNIQUE,
    FOREIGN KEY(STDNT_ID) REFERENCES STUDENT(STDNT_ID)
);

Student hbm.xml

<hibernate-mapping>
   <class name="Student" table="STDT">
   <id name="studentId" column="ID"</id>
   <property name="name" column="NAME" update="false" type="string" />
   <property name="firstName" column="FIRST_NAME"/>
   <property name="lastName" column="LAST_NAME"/>

   <one-to-one name="address" lazy="false" fetch="join" class="com.orgname.projectname.Address"></one-to-one>
    </class>
  <hibernate-mapping>

Address hbm.xml

 <hibernate-mapping>
  <class name="Address" table="ADDRESS">
   <id name="addressId" column="A_ID"</id>
   <property name="streetAddress" column="STREET_ADDRESS"/>
   <property name="addressLine1" column="ADDR1"/>
   <property name="addressLine2" column="ADDR2"/>
   <property name="city" column="CITY"/>
   <property name="state" column="STATE"/>
   <property name="country" column="COUNTRY"/>
   <property name="zipCode" column="ZIPCODE"/>
   <property name="student_id" column="STDNT_ID"/>
  </class>
 <hibernate-mapping>

I would like to know what needs to be done, in order for me to include Address while retrieving Student via a GET end point.

1

There are 1 best solutions below

1
sgraton On

You need to declare the 1:1 relationship in both entities. Otherwise, when you save the address attached to the student ID, hibernate considers it as a value in "STDNT_ID" column but not a foreign key. The relationship between the two entities is therefore not made.

<class name="Address" table="ADDRESS">
   ...
   <one-to-one name="student" constrained="true" class="Student" />
</class>

And in your entity :

public class Address{
   ...
   private Student student;
}

You can find a similar example here: https://www.codejava.net/frameworks/hibernate/hibernate-one-to-one-with-primary-key-xml-mapping-example