简体   繁体   English

如何使用 Criteria Query 查找带有外键的记录。 它是休眠的一对一双向映射,外键使用 hbm.xml 完成

[英]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).我正在尝试根据外键(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.寻求帮助编写标准查询以根据学生 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学生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地址 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]见这里:[ https://stackoverflow.com/a/1787161/333296]

You should be able to get the address of a student with student.id .您应该能够通过student.id获取学生的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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