一、创建Java工程,新建Lib文件夹,加入Hibernate和数据库(如MySql、Oracle、SqlServer等)的Jar包,创建 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.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- <property name="connection.url">jdbc:mysql:///mis</property> -->
<property name="connection.url">
<![CDATA[jdbc:mysql://localhost:3306/mis?useUnicode=true&characterEncoding=utf8]]>
</property> <!-- 配置 hibernate 的基本信息 -->
<!-- hibernate 所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 设置 Hibernate 的事务隔离级别 :读已提交的记录-->
<property name="connection.isolation"></property> <!-- 删除对象后, 使其 OID 置为 null -->
<property name="use_identifier_rollback">true</property> <!-- 配置 C3P0 数据源 -->
<property name="hibernate.c3p0.max_size"></property>
<property name="hibernate.c3p0.min_size"></property>
<property name="c3p0.acquire_increment"></property> <property name="c3p0.idle_test_period"></property>
<property name="c3p0.timeout"></property> <property name="c3p0.max_statements"></property> <!-- 设定 JDBC 的 Statement 读取数据的时候每次从数据库中取出的记录条数 -->
<property name="hibernate.jdbc.fetch_size"></property> <!-- 设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
<property name="jdbc.batch_size"></property> <!-- 指定关联的 .hbm.xml 文件 -->
<!--
<mapping resource="com/mcs/hibernate/entities/onetoone/foreign/Manager.hbm.xml" />
<mapping resource="com/mcs/hibernate/entities/onetoone/foreign/Department.hbm.xml" />
-->
<!--
<mapping resource="com/mcs/hibernate/entities/onetoone/primary/Manager.hbm.xml" />
<mapping resource="com/mcs/hibernate/entities/onetoone/primary/Department.hbm.xml" />
-->
<!--
<mapping resource="com/mcs/hibernate/entities/manytomany/Category.hbm.xml" />
<mapping resource="com/mcs/hibernate/entities/manytomany/Product.hbm.xml" />
-->
<!--
<mapping resource="com/mcs/hibernate/entities/subclass/Person.hbm.xml" />
-->
<!--
<mapping resource="com/mcs/hibernate/entities/joined/subclass/Person.hbm.xml" />
--> <mapping resource="com/mcs/hibernate/entities/union/subclass/Person.hbm.xml" /> </session-factory> </hibernate-configuration>

二、建立多对多的映射

1、创建Java实体类

 package com.mcs.hibernate.entities.manytomany;

 import java.util.HashSet;
import java.util.Set; public class Category { private Integer id;
private String name; private Set<Product> products = new HashSet<>(); public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Set<Product> getProducts() {
return products;
} public void setProducts(Set<Product> products) {
this.products = products;
} }
 package com.mcs.hibernate.entities.manytomany;

 import java.util.HashSet;
import java.util.Set; public class Product { private Integer id;
private String name; private Set<Category> categories = new HashSet<>(); public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Category> getCategories() {
return categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
} }

2、根据实体类创建对应的 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">
<!-- Generated -- :: by Hibernate Tools 3.5..Final -->
<hibernate-mapping>
<class name="com.mcs.hibernate.entities.manytomany.Category" table="CATEGORIES">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property> <!-- table: 指定中间表 -->
<set name="products" table="CATEGORIES_PRODUCTS" inverse="true">
<key>
<column name="CATEGORY_ID" />
</key>
<!-- 使用 many-to-many 指定多对多的关联关系. column 执行 Set 集合中的持久化类在中间表的外键列的名称 -->
<many-to-many class="com.mcs.hibernate.entities.manytomany.Product" column="PRODUCT_ID"/>
</set>
</class>
</hibernate-mapping>
 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated -- :: by Hibernate Tools 3.5..Final -->
<hibernate-mapping>
<class name="com.mcs.hibernate.entities.manytomany.Product" table="PRODUCTS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property> <!-- table: 指定中间表 -->
<set name="categories" table="CATEGORIES_PRODUCTS">
<key>
<column name="PRODUCT_ID" />
</key>
<!-- 使用 many-to-many 指定多对多的关联关系. column 执行 Set 集合中的持久化类在中间表的外键列的名称 -->
<many-to-many class="com.mcs.hibernate.entities.manytomany.Category" column="CATEGORY_ID"/>
</set>
</class>
</hibernate-mapping>

3、备注:

  1、使用 many-to-many 指定多对多的关联关系.

    table: 指定中间表

    Key:当前持久化类在中间表的外键列的名称

    column:执行 Set 集合中的持久化类在中间表的外键列的名称

  2、为了不重复更新,在其中的一端设置 inverse="true"的属性

  3、在查询时需要连接中间表

05-27 22:25