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

hibernate one-to-one foreign key association

巴扎黑
Release: 2016-12-10 09:39:48
Original
976 people have browsed it

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 + "]";  
    }  
  
}
Copy after login

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 + "]";  
    }  
  
}
Copy after login

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>
Copy after login

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>
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template