Hibernate关联映射之一对多单向关联映射

来源:互联网 发布:js模块化有什么好处 编辑:程序博客网 时间:2024/06/04 18:33

此实例为一对多(Depart——Employee)单向关联映射

方法一:使用Annotation实现

(1)建立Depart类和Employee类

package com.model;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;@Entitypublic class Depart {private int id;private String name;private Set<Employee> employees = new HashSet<Employee>();@Idpublic 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;}@OneToMany@JoinColumn(name="depart_id")public Set<Employee> getEmployees() {return employees;}public void setEmployees(Set<Employee> employees) {this.employees = employees;}}
package com.model;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class Employee {private int id;private String name;@Idpublic 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;}}

(2)设置Hibernate配置文件hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- 连接的数据库驱动 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- 连接的数据库的url --><property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property><!-- 连接的数据库的用户名--><property name="connection.username">root</property><!-- 连接的数据库的密码 --><property name="connection.password"></property><!-- 配置Hibernate数据库方言 --><property name="Dialect">org.hibernate.dialect.MySQLDialect</property><!-- 输出执行的SQL语句 -->        <property name="show_sql">true</property>        <property name="format_sql">true</property><!-- 启动时撤销并重新创建数据库的模式--><property name="hbm2ddl.auto">create</property><property name="current_session_context_class">thread</property><mapping class="com.model.Depart"/><mapping class="com.model.Employee"/></session-factory></hibernate-configuration>

(3)建立测试类,此处使用Junit4进行测试,仅仅测试一对一映射如何建表即可。

package com.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.Test;public class ORMappingTest {@Testpublic void test() {Configuration cfg = new Configuration();cfg.configure();ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory  sf = cfg.buildSessionFactory(sr);Session s = sf.getCurrentSession();Transaction tx = s.beginTransaction();tx.commit();sf.close();}}

(4)运行测试类,查看后台输出的建表SQL语句。

Hibernate:     create table Depart (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )Hibernate:     create table Employee (        id integer not null auto_increment,        name varchar(255),        depart_id integer,        primary key (id)    )Hibernate:     alter table Employee         add index FK4AFD4ACE7F7C470C (depart_id),         add constraint FK4AFD4ACE7F7C470C         foreign key (depart_id)         references Depart (id)

--------------Depart类中Employee的getter方法必须声明注解@JoinColumn(name="departId"),在表Employee中生成外键字段departId。

--------------若不声明注解,则会自动按照多对多的思想生成中间表Depart_Employee,结果如下:

Hibernate:     create table Depart (        id integer not null,        name varchar(255),        primary key (id)    )Hibernate:     create table Depart_Employee (        Depart_id integer not null,        employees_id integer not null,        primary key (Depart_id, employees_id),        unique (employees_id)    )Hibernate:     create table Employee (        id integer not null,        name varchar(255),        primary key (id)    )Hibernate:     alter table Depart_Employee         add index FK8DEC9E997F7C470C (Depart_id),         add constraint FK8DEC9E997F7C470C         foreign key (Depart_id)         references Depart (id)Hibernate:     alter table Depart_Employee         add index FK8DEC9E991BC69C15 (employees_id),         add constraint FK8DEC9E991BC69C15         foreign key (employees_id)         references Employee (id)

方法二:使用映射文件hbm.xml实现:

(1)建立Depart类和Employee类,在方法一的基础上去掉@注解即可。

(2)建立映射文件Depart.hbm.xml和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="com.model">    <class name="Depart">    <id name="id">    <generator class="native"></generator>    </id>            <property name="name" />              <set name="employees">            <key column="depart_id"></key>            <one-to-many class="Employee"/>            </set>     </class>  </hibernate-mapping>
<?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="com.model">    <class name="Employee">    <id name="id">    <generator class="native"></generator>    </id>            <property name="name" />    </class>  </hibernate-mapping>

(3)设置Hibernate配置文件hibernate.cfg.cml的映射文件

<mapping resource="com/model/Depart.hbm.xml"/> <mapping resource="com/model/Employee.hbm.xml"/> 

(4)运行方法一的测试类,得到相同的测试结果。

原创粉丝点击