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 class="native"/> </id> <property name="name"/> <set name="emps"> <key column="dept_id"></key> <one-to-many class="com.cos.entity.Employee"/> </set> </class> </hibernate-mapping>
set标签中的name=emps是Department对象中的属性值。
key标签中的column="dept_id"是Department对应表中的字段值。
one-to-many标签中的class值是具体的对象。
<?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 class="native"/>
</id>
<property name="name"/>
<many-to-one name="dept" 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
本文介绍Hibernate框架中的一对多关联映射实现方式,通过Department和Employee两个实体类演示如何建立和配置一对多的关系,并提供了完整的代码示例。
1214

被折叠的 条评论
为什么被折叠?



