当前位置:编程学习 > XML/UML >>

Hibernate的一对多XML映射教程

1。创建数据库
在这个例子中,我们将MySQL数据库。创建下面两个表在MySQL。需要注意的是Employee和Department表显示出一个一对多的关系。每个部门可以assosiated多个员工,每个员工只能有一个部门。

一个关系图
CREATE TABLE `department` (
 `department_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
 `dept_name` VARCHAR(50) NOT NULL DEFAULT '0',
 PRIMARY KEY (`department_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=115

 

CREATE TABLE `employee` (
 `employee_id` BIGINT(10) NOT NULL AUTO_INCREMENT,
 `firstname` VARCHAR(50) NULL DEFAULT NULL,
 `lastname` VARCHAR(50) NULL DEFAULT NULL,
 `birth_date` DATE NULL DEFAULT NULL,
 `cell_phone` VARCHAR(15) NULL DEFAULT NULL,
 `department_id` BIGINT(20) NULL DEFAULT NULL,
 PRIMARY KEY (`employee_id`),
 INDEX `FK_DEPT` (`department_id`),
 CONSTRAINT `FK_DEPT` FOREIGN KEY (`department_id`) REFERENCES `department` (`department_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
2。Hibernate的Maven的依赖关系
我们正在使用Maven的依赖管理。复制后,在pom.xml。

文件:pom.xml的

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>net.viralpatel.hibernate</groupId>
 <artifactId>HibernateHelloWorldXML</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>HibernateHelloWorldXML</name>
 <url>http://maven.apache.org</url>
 <dependencies>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.10</version>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate</artifactId>
   <version>3.2.6.ga</version>
  </dependency>
 </dependencies>
</project>
3。Hibernate的模型类
员工和部门创建模型类映射到相应的数据库表。

文件“:Department.java”

package net.viralpatel.hibernate;

import java.util.Set;

public class Department {

 private Long departmentId;
 
 private String departmentName;
 
 private Set<Employee> employees;

 // Getter and Setter methods
}
文件“:Employee.java”

package net.viralpatel.hibernate;

import java.sql.Date;

public class Employee {

 private Long employeeId;

 private String firstname;

 private String lastname;

 private Date birthDate;

 private String cellphone;

 private Department department;

 public Employee() {
 }

 public Employee(String firstname, String lastname, Date birthdate,
   String phone) {
  this.firstname = firstname;
  this.lastname = lastname;
  this.birthDate = birthdate;
  this.cellphone = phone;
 }

 // Getter and Setter methods
}
4。Hibernate工具类
要访问Hibernate的API,我们将创建一个包装器实用工具类,它为我们提供了SessionFactory的。

文件“:HibernateUtil.java”

package net.viralpatel.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();

 private static SessionFactory buildSessionFactory() {
  try {
   // Create the SessionFactory from hibernate.cfg.xml
   return new Configuration().configure().buildSessionFactory();
  } catch (Throwable ex) {
   System.err.println("Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }
}
5。Hibernate映射XML(HBM)
以下是为每个enitity员工和部门的Hibernate映射文件。

文件“: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 package="net.viralpatel.hibernate">

 <class name="Employee" table="EMPLOYEE">
  <id name="employeeId" column="EMPLOYEE_ID">
   <generator class="native" />
  </id>

  <property name="firstname" />
  <property name="lastname" column="lastname" />
  <property name="birthDate" type="date" column="birth_date" />
  <property name="cellphone" column="cell_phone" />


    <many-to-one name="department" class="net.viralpatel.hibernate.Department" fetch="select">
            <column name="department_id" not-null="true" />
        </many-to-one>

 </class>
</hibernate-mapping>
文件“: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 package="net.viralpatel.hibernate">

    <class name="Department" table="DEPARTMENT">

   <id name="departmentId" type="java.lang.Long" column="DEPARTMENT_ID" >
   <generator class="native" />
  </id>
  
        <property name="departmentName" column="DEPT_NAME"/>

  <set name="employees" table="employee"
    inverse="true" lazy="true" fetch="select">
            <key>
                <column name="department_id" not-null="true" /&g

补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,