简体   繁体   中英

How to find records with Foreign Key using Criteria Query. It's hibernate One-to-One Bidirectional mapping with Foreign Key done using hbm.xml

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, this makes it a One-to-One bi-directional mapping.

I am trying to retrieve Address records based on the Foreign Key(studentId). I need to write a criteria query for this. Since in a One-to-one bidirectional mapping we don't declare the Foreign key as a field in the Address Entity class, I am unable to add Restrictions, which otherwise if added would query for the records based on the Foreign key. Looking for Help in writing Criteria Query to Fetch Address records based on Student ID.

    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;
    private Student student
    //getters and setters
   }



  public interface AddressDao{

   //Find Address based on ForeignKey
   public Address findByStudentId(String studentId)
   }

   public class AddressDaoImpl{

   @Override
    public Address findByStudentId(String studentId) {
    Criteria criteria = createCriteria();
    criteria.add(Restrictions.eq("Should be the column name of the Field holding Foreign key", studentId)); // haven't declared the student ID field in the Address POJO
    Object result = criteria.uniqueResult();
    return result == null ? null : (Address) result;
   }

STUDENT TABLE

  CREATE TABLE STUDENT (
        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,
        ID VARCHAR(20) UNIQUE,
        FOREIGN KEY(ID) REFERENCES STUDENT(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" cascade="all"></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"/>

       <many-to-one name="student" column="ID" unique="true"
        not-null="true" lazy="false" />
    </class>
<hibernate-mapping>

See here: [ https://stackoverflow.com/a/1787161/333296]

You should be able to get the address of a student with student.id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM