Hibernate单向无连接表N-1关联的简单示例

来源:互联网 发布:vim for mac os x 编辑:程序博客网 时间:2024/05/01 12:27

1. 单向N-1关系,比如多个职员对应同一个部门,只需从职员实体可以找到对应的部门实体,无须关心某个部门的全部职员。


2. 部门实体Department和职员实体Employee:

package com.huey.entity;/** * 部门实体 * @author Huey2672 * */public class Department {private Integer deptId;private String deptName;public Integer getDeptId() {return deptId;}public void setDeptId(Integer deptId) {this.deptId = deptId;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}public Department() {}public Department(String deptName) {setDeptName(deptName);}public Department(int deptId, String deptName) {this(deptName);setDeptId(deptId);}}
package com.huey.entity;/** * 职员实体 * @author Huey2672 * */public class Employee {private Integer empId;private String empName;private Department department;public Integer getEmpId() {return empId;}public void setEmpId(Integer empId) {this.empId = empId;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}public Employee() {}public Employee(String empName, Department department) {this.empName = empName;this.department = department;}}

3. 在数据库中创建表和序列(Oracle):

create table tab_dept(dept_id number(8) primary key,dept_name varchar2(20) not null);create table tab_emp(emp_id number(8) primary key,emp_name varchar2(20) not null,dept_id number(8),foreign key(dept_id) references tab_dept(dept_id));create sequence dept_id_sequencestart with 1001increment by 1;create sequence emp_id_sequencestart with 1002increment by 1;

4. 配置映射文件Department.hbm.xml和Employee.hbm.xml:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huey.entity"><class name="Department" table="tab_dept"><id name="deptId" column="dept_id"><generator class="sequence"><param name="sequence">DEPT_ID_SEQUENCE</param></generator></id><property name="deptName" column="dept_name" /></class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huey.entity"><class name="Employee" table="tab_emp"><id name="empId" column="emp_id"><generator class="sequence"><param name="sequence">EMP_ID_SEQUENCE</param></generator></id><property name="empName" column="emp_name" /><!-- 映射N-1关联实体,并指定级联全部操作  --><many-to-one name="department" column="dept_id" class="Department" cascade="all"/></class></hibernate-mapping>

5. 配置配置文件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><!-- Database connection settings --><property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><property name="connection.username">oa</property><property name="connection.password">oa</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.Oracle10gDialect</property><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property><mapping resource="com/huey/entity/mapping/Department.hbm.xml" /><mapping resource="com/huey/entity/mapping/Employee.hbm.xml" /></session-factory></hibernate-configuration>

6. 测试用例:

package com.huey.test;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.Test;import com.huey.entity.Department;import com.huey.entity.Employee;/** *  * @author Huey2672 * */public class EmployeeTest {private static SessionFactory sf;static {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");ServiceRegistryBuilder srb = new ServiceRegistryBuilder().applySettings(configuration.getProperties());sf = configuration.buildSessionFactory(srb.buildServiceRegistry());}@Testpublic void testAddDepartment() throws Exception {Session session = sf.openSession();Transaction transaction = null;try {transaction = session.beginTransaction();Department department = new Department("国防部");Employee huey = new Employee("huey", department);Employee sugar = new Employee("sugar", department);session.save(huey);session.save(sugar);transaction.commit();} catch (HibernateException e) {if (transaction != null) {transaction.rollback();}e.printStackTrace();} finally {if (session != null) {session.close();}}}}

7. 可以看到数据表tab_dept中新插入一条记录,tab_emp中新插入了两条记录。

原创粉丝点击