最近看了下hibernate的映射关系,看了网上的许多介绍,感觉比较复杂.个人在用的过程中认为所有的关系都认为1:n(n>=1).搞不懂为什么会有那么多复杂的映射关系,可能对hibernate的工作原理理解的还不是太透彻.
下面是个人关于雇员和部门学习的一个简单示例:
CREATE TABLE `employee` (
`empid` int(11) NOT NULL AUTO_INCREMENT,
`ename` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
PRIMARY KEY (`empid`),
KEY `deptid` (`deptid`)
) ENGINE=InnoDB AUTO_INCREM
CREATE TABLE `department` (
`deptid` int(11) NOT NULL AUTO_INCREMENT,
`dname` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`location` varchar(40) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`deptid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=gbk
Employee.java类
package com.bean;
import java.io.Serializable;
/**
* @author chen
*
*/
public class Employee implements Serializable {
private Integer empid;
private Department department;
private String ename;
/** default constructor */
public Employee()
{
}
/** full constructor */
public Employee(Department department, String ename)
{
this.department = department;
this.ename = ename;
}
public Integer getEmpid() {
return empid;
}
public void setEmpid(Integer empid) {
this.empid = empid;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.bean.Employee" table="employee">
<id name="empid" type="java.lang.Integer">
<column name="empid" />
<generator class="identity" />
</id>
<property name="ename" type="java.lang.String">
<column name="ename" length="20" />
</property>
<many-to-one name="department" class="com.bean.Department" cascade="save-update">
<column name="deptid" />
</many-to-one>
</class>
</hibernate-mapping>
Department .java类
package com.bean;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* @ *
*/
public class Department implements Serializable {
private Integer deptid;
private String dname;
private String location;
private Set employees = new HashSet(0);
public Department()
{
}
/** full constructor */
public Department(String dname, String location, Set employees)
{
this.dname = dname;
this.location = location;
this.employees = employees;
}
public Integer getDeptid() {
return deptid;
}
public void setDeptid(Integer deptid) {
this.deptid = deptid;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Set getEmployees() {
return employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
}
Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.bean.Department" table="department">
<id name="deptid" type="java.lang.Integer">
<column name="deptid" />
<generator class="identity" />
</id>
<property name="dname" type="java.lang.String">
<column name="dname" length="20" />
</property>
<property name="location" type="java.lang.String">
<column name="location" length="40" />
</property>
<set name="employees" cascade="save-update">
<key>
<column name="deptid" />
</key>
<one-to-many class="com.bean.Employee" />
</set>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 方言(dialect)的property元素指明Hibernate 生成的特定SQL变量 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 显示sql语句-->
<property name="show_sql">true</property>
<mapping resource="com/bean/Employee.hbm.xml" />
<mapping resource="com/bean/Department.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类:
package com.app;
import java.util.Date;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.bean.Department;
import com.bean.Employee;
import com.factory.HibernateSessionFactory;
public class Test
{
// 演示hibernate session 的save()
public void testSaveEmp()
{
Employee emp1 = new Employee();
emp1.setEname("kaka");
Employee emp2 = new Employee();
emp2.setEname("peter2");
Department dept1 = new Department();
dept1.setDname("管理部");
emp1.setDepartment(dept1);
emp2.setDepartment(dept1);
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(emp1);
session.save(emp2);
tx.commit();
HibernateSessionFactory.closeSession();
}
public void testSaveDept()
{
Employee emp1 = new Employee();
emp1.setEname("king");
Employee emp2 = new Employee();
emp2.setEname("peter");
Department dept1 = new Department();
dept1.setDname("设计部");
dept1.getEmployees().add(emp1);
dept1.getEmployees().add(emp2);
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(dept1);
tx.commit();
HibernateSessionFactory.closeSession();
}
/**
* @param args
*/
public static void main(String[] args)
{
Test t = new Test();
t.testSaveDept();
}
}