JPA关系映射之one-to-many和many-to-one

one-to-many(一对多)和many-to-one(多对一)双向关联

假设部门与员工是一对多关系,反过来员工与部门就是多对一关系。

Dept.java类

 public class Dept implements java.io.Serializable {

     // Fields

     private Integer deptId;
private String deptName;
private Set<Emp> emps=new HashSet<Emp>(); // Constructors /** default constructor */
public Dept() {
} /** full constructor */
public Dept(String deptName) {
this.deptName = deptName;
} // Property accessors public Integer getDeptId() {
return this.deptId;
} public void setDeptId(Integer deptId) {
this.deptId = deptId;
} public String getDeptName() {
return this.deptName;
} public void setDeptName(String deptName) {
this.deptName = deptName;
} public Set<Emp> getEmps() {
return emps;
} public void setEmps(Set<Emp> emps) {
this.emps = emps;
} }

Emp.java类

 public class Emp implements java.io.Serializable {

     // Fields

     private Integer empNo;
private String empName;
private Date empBrithday;
private Dept dept; // Constructors /** default constructor */
public Emp() {
} /** full constructor */
public Emp(String empName, Date empBrithday) {
this.empName = empName;
this.empBrithday = empBrithday;
} // Property accessors public Integer getEmpNo() {
return this.empNo;
} public void setEmpNo(Integer empNo) {
this.empNo = empNo;
} public String getEmpName() {
return this.empName;
} public void setEmpName(String empName) {
this.empName = empName;
} public Date getEmpBrithday() {
return this.empBrithday;
} public void setEmpBrithday(Date empBrithday) {
this.empBrithday = empBrithday;
} public Dept getDept() {
return dept;
} public void setDept(Dept dept) {
this.dept = dept;
} }

Dept.hbm.xml

 <hibernate-mapping>
<class name="com.db.entity.Dept" table="dept" catalog="mydb">
<id name="deptId" type="java.lang.Integer">
<column name="deptId" />
<generator class="native" />
</id>
<property name="deptName" type="java.lang.String">
<column name="deptName" length="32" />
</property>
<set name="emps" inverse="true" cascade="all">
<key column="deptId" not-null="true" />
<one-to-many class="com.db.entity.Emp" />
</set>
</class>
</hibernate-mapping>

Emp.hbm.xml

 <hibernate-mapping>
<class name="com.db.entity.Emp" table="emp" catalog="mydb">
<id name="empNo" type="java.lang.Integer">
<column name="empNo" />
<generator class="native" />
</id>
<property name="empName" type="java.lang.String">
<column name="empName" length="20" />
</property>
<property name="empBrithday" type="java.util.Date">
<column name="empBrithday"/>
</property>
<many-to-one name="dept" column="deptId" class="com.db.entity.Dept" not-null="true" fetch="select" cascade="save-update,delete"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml

 <hibernate-configuration>

     <session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/mydb
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
MyDBAccount
</property>
<mapping resource="com/db/entity/Dept.hbm.xml" />
<mapping resource="com/db/entity/Emp.hbm.xml" />
</session-factory> </hibernate-configuration>

测试用例

 public class TestOneMany {

     public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub Session session=HibernateSessionFactory.getSession();
Dept dept1=new Dept();
dept1.setDeptName("开发部");
Emp emp1=new Emp();
emp1.setEmpName("王洋");
String brithString="1999-03-05";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date brithday=sdf.parse(brithString);
emp1.setEmpBrithday(brithday);
Emp emp2=new Emp();
emp2.setEmpName("李林");
brithString="2005-02-07";
brithday=sdf.parse(brithString);
emp2.setEmpBrithday(brithday);
//把emp和dpt分别添加到对方的实力对象中
dept1.getEmps().add(emp1);
dept1.getEmps().add(emp2);
emp1.setDept(dept1);
emp2.setDept(dept1);
session.beginTransaction();
session.save(dept1);
session.getTransaction().commit();
} }

注意:

在一的set配置中声明inverse=“true”,意味着Dept不在作为主控方,将关联关系的维护工作交给关联对象Emp来完成。在保存dept1对象的时不在关心Emp类中dept属性对应的deptId列,必须由Emp自己去维护,即设置emp.setDept(dept)

上述操作完成的sql语句是:

Hibernate: insert into mydb.dept (deptName) values (?)
Hibernate: insert into mydb.emp (empName, empBrithday, deptId) values (?, ?, ?)
Hibernate: insert into mydb.emp (empName, empBrithday, deptId) values (?, ?, ?)

上一篇:Android系统--输入系统(十二)Dispatch线程_总体框架


下一篇:JPA关系映射之many-to-many