首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate 联系关系映射之一对多

2012-08-21 
Hibernate 关联映射之一对多Hibernate的关联映射包括:一对一(Persion - IDCard)一对多(Department - Emplo

Hibernate 关联映射之一对多

Hibernate的关联映射包括:

一对一(Persion - IDCard)

一对多(Department - Employee)

多对一(Employee - Department)

多对多(Teacher - Student)

组件映射(User - Name)

集合映射(Set、List、Map)

inverse和cascade(Employee - Department)

?

本文以一对多为例:

一个部门对应多个员工(Department - Employee)

映射文件 <many-to-one name="depart" column="depart_id">

?

实体类:Department.java 和 Employee.java

package com.cos.entity;import java.util.Set;/** * 部门 * @author wangjy */public class Department {private int id;private String name;private Set<Employee> emps;//一对多public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Employee> getEmps() {return emps;}public void setEmps(Set<Employee> emps) {this.emps = emps;}}
package com.cos.entity;/** * 员工 * @author wangjy */public class Employee {private int id;private String name;private Department dept;//多对一public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Department getDept() {return dept;}public void setDept(Department dept) {this.dept = dept;}}

?映射文件:Department.hbm.xml 和 Employee.hbm.xml

<?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="com.cos.entity.Department" lazy="true"><id name="id"><generator name="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><class name="com.cos.entity.Employee" lazy="true"><id name="id"><generator column="dept_id"/></class></hibernate-mapping>

Hibernate配置文件:hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql:///hi</property>        <property name="hibernate.connection.username"></property>        <property name="hibernate.connection.password"></property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="hibernate.hbm2ddl.auto">validate</property>        <property name="show_sql">true</property><mapping resource="com/cos/entity/Employee.hbm.xml"/><mapping resource="com/cos/entity/Department.hbm.xml"/></session-factory></hibernate-configuration>

工具类:HibernateUtil.java

package com.cos.util;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public final class HibernateUtil {private static SessionFactory sesseionFactory;static {Configuration conf = new Configuration();conf.configure();sesseionFactory = conf.buildSessionFactory();}public static SessionFactory getSesseionFactory() {return sesseionFactory;}}

测试类:

package com.cos.main;import java.util.HashSet;import java.util.Set;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import com.cos.entity.Department;import com.cos.entity.Employee;import com.cos.util.HibernateUtil;public class One2Many {public static void main(String[] args) {add();}public static void add(){SessionFactory  sessionFactory = null;Session s = null;Transaction t = null;try{sessionFactory = HibernateUtil.getSesseionFactory();s = sessionFactory.openSession();t = s.beginTransaction();Department dept = new Department();dept.setName("dept name 3");Employee emp1 = new Employee();emp1.setName("emp name 3");emp1.setDept(dept);Employee emp2 = new Employee();emp2.setName("emp name 33");emp2.setDept(dept);s.save(emp1);s.save(emp2);Set<Employee> set = new HashSet<Employee>();set.add(emp1);set.add(emp2);dept.setEmps(set);s.save(dept);t.commit();s.close();sessionFactory.close();}catch(Exception e){e.printStackTrace();}}}

表结构:department表 和employee表

CREATE TABLE `department` (     `id` int(11) NOT NULL auto_increment,     `name` varchar(255) default NULL,     PRIMARY KEY  (`id`)   ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1     CREATE TABLE `employee` (     `id` int(11) NOT NULL auto_increment,     `name` varchar(255) default NULL,     `dept` int(11) default NULL,     PRIMARY KEY  (`id`),     KEY `FK4AFD4ACE66A70F4E` (`dept`),     CONSTRAINT `FK4AFD4ACE66A70F4E` FOREIGN KEY (`dept`) REFERENCES `department` (`id`)   ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1   

?

热点排行