Home  >  Article  >  Java  >  hibernate one-to-one foreign key association

hibernate one-to-one foreign key association

巴扎黑
巴扎黑Original
2016-12-10 09:39:48941browse

1. Create entity class

Java code

package com.wr.hibernate.entity;  
  
public class Department {  
  
    private int deptId;  
    private String deptName;  
    private Manager mgr;  
  
    public int getDeptId() {  
        return deptId;  
    }  
  
    public Manager getMgr() {  
        return mgr;  
    }  
  
    public void setMgr(Manager mgr) {  
        this.mgr = mgr;  
    }  
  
    public void setDeptId(int deptId) {  
        this.deptId = deptId;  
    }  
  
    public String getDeptName() {  
        return deptName;  
    }  
  
    public void setDeptName(String deptName) {  
        this.deptName = deptName;  
    }  
  
    @Override  
    public String toString() {  
        return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";  
    }  
  
}

Java code

package com.wr.hibernate.entity;  
  
public class Manager {  
  
    private int mgrId;  
    private String mgrName;  
    private Department dept;  
  
    public int getMgrId() {  
        return mgrId;  
    }  
  
    public void setMgrId(int mgrId) {  
        this.mgrId = mgrId;  
    }  
  
    public String getMgrName() {  
        return mgrName;  
    }  
  
    public void setMgrName(String mgrName) {  
        this.mgrName = mgrName;  
    }  
  
    public Department getDept() {  
        return dept;  
    }  
  
    public void setDept(Department dept) {  
        this.dept = dept;  
    }  
  
    @Override  
    public String toString() {  
        return "Manager [mgrId=" + mgrId + ", mgrName=" + mgrName + ", dept=" + dept + "]";  
    }  
  
}

2. Create POJO mapping file

Xml code

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.wr.hibernate.entity">  
  
    <class name="Department" table="department" dynamic-insert="true">  
  
        <id name="deptId" type="java.lang.Integer">  
            <column name="dept_id" />  
            <generator class="native"></generator>  
        </id>  
  
        <property name="deptName" type="java.lang.String">  
            <column name="dept_name" />  
        </property>  
          
        <many-to-one name="mgr" class="Manager">  
            <column name="mgr_id" unique="true"></column>  
        </many-to-one>  
          
          
    </class>  
  
</hibernate-mapping>

Xml code

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.wr.hibernate.entity">  
  
    <class name="Manager" table="manager" dynamic-insert="true">  
  
        <id name="mgrId" type="java.lang.Integer">  
            <column name="mgr_id" />  
            <generator class="native"></generator>  
        </id>  
  
        <property name="mgrName" type="java.lang.String">  
            <column name="mgr_name" />  
        </property>  
          
        <one-to-one name="dept" class="Department" property-ref="mgr">  
        </one-to-one>  
  
    </class>  
  
</hibernate-mapping>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Common classes in JavaNext article:Common classes in Java