首页 > 代码库 > 图解、详解Hibernate多对一映射

图解、详解Hibernate多对一映射

步骤1:案例代码。

步骤2:案例图解分析。

------------------------------------------------------------------------步骤1:案例代码-------------------------------------------------------------------------------------------------------------------

我们讲解Hibernate多对一映射的时候,首先想到的就是经典的多对一的关系,员工和部门,下面我们就来看看他们是如何映射的。

代码清单1:员工类

package 对象一对多;

public class Employment {
	private Integer id;
	private String name;
	private Department department;
	
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "[员工编号:"+this.id+", 员工姓名:"+this.name+"]";
	}
}


代码清单2:部门类

package 对象一对多;

import java.util.HashSet;
import java.util.Set;

public class Department {
	private Integer id;
	private String name;
	private Set<Employment> empSet = new HashSet<Employment>();
	
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Employment> getEmpSet() {
		return empSet;
	}

	public void setEmpSet(Set<Employment> empSet) {
		this.empSet = empSet;
	}

	@Override
	public String toString() {
		return "[部门 编号:"+this.id+",部门名称:"+this.name+"]";
	}
	

}


代码清单3:员工映射文件

<?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>
   <class name="对象一对多.Employment">
      <id name="id">
        <generator class="native"/>
      </id>
      <property name="name"/>
      <many-to-one name="department" class="对象一对多.Department" column="departmentId"/>
   </class>
</hibernate-mapping>


代码清单4:部门映射文件

<?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>
	<class name="对象一对多.Department">
	    <id name="id">
	      <generator class="native"/>
	    </id>
	    <property name="name"/>
	    <set name="empSet">
	      <key column="departmentId"/>
	      <one-to-many class="对象一对多.Employment"/>
	    </set>
	</class>
</hibernate-mapping>


代码清单5:客户执行

package 对象一对多;

import org.hibernate.Session;
import org.hibernate.Transaction;

import Utils.HibernateUtil;

public class Client {
	public static void main(String[] args) {
		Client.save();
	}
	public static void save() {
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateUtil.getSession();
			transaction = session.beginTransaction();
			
			
			//接触关联
			Department department=(Department) session.get(Department.class, 1);
			Employment employment=(Employment) session.get(Employment.class,department.getId());
			employment.setDepartment(null);
			
			
            System.out.println(department);            
            System.err.println(department.getEmpSet());
			
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}



-------------------------------------------------------------------------步骤2:案例图解分析---------------------------------------------------------------------------------------------------------------

首先是先插入部门表, 再插入员工表。 



如果是先插入员工表,在插入部门表呢?


辛苦了大半个晚上了,希望对大家有帮助,我就很开心了。