如何使用条件查询查找带有外键的记录。使用hbm.xml

时间:2019-02-11 21:03:43

标签: java spring hibernate hibernate-criteria hbmxml

我有两个实体,即学生和地址。父实体是学生,子实体是地址。考虑到每个学生只能有一个地址,而每个地址只能属于一个学生,因此这使其成为一对一的双向映射。

我正在尝试根据外键(studentId)检索地址记录。我需要为此编写条件查询。由于在一对一的双向映射中,我们没有在地址实体类中将外键声明为字段,所以我无法添加限制,否则如果添加限制将基于外键查询记录。在编写标准查询以基于学生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;
   }

学生桌

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

地址表

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

学生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>

地址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>

1 个答案:

答案 0 :(得分:0)

查看此处:[https://stackoverflow.com/a/1787161/333296]

您应该可以使用student.id来获取学生的地址。