一、创建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、在查询时需要连接中间表

Hibernate的多对多映射的更多相关文章

  1. Hibernate的多对一映射

    一.创建Java工程,新建Lib文件夹,加入Hibernate和数据库(如MySql.Oracle.SqlServer等)的Jar包,创建 hibernate.cfg.xml 文件,并配置,配置项如下 ...

  2. Hibernate的多对多映射关系

    example: 老师(teacher)和学生(Student)就是一个多对多的关系吧?老师可以有多个学生,学生也可以由多个老师,那在Hibernate中多对多是怎样实现的呢?? 在Hibernate ...

  3. hibernate单向多对一映射

    n21: 1.new 两个实体类,一个代表"多"的一端,一个代表"一"的一端. Customer类: public class Customer { priva ...

  4. hibernate之多对多映射

    目录 第一章 多对多的应用场景 第二章 多对多的映射配置案例 2-1 创建项目和表 2-2 创建持久化类和映射文件 2-3 配置映射文件 2-4 测试 第三章 总结 源码地址:https://gith ...

  5. hibernate 2 多对多映射

    一.实体类 1.Classes.java package cn.gs.wwg.entity; import java.util.Set; public class Classes { private ...

  6. Hibernate(八)多对多映射

    一.创建数据表 --学生证表 create table paper ( pid number primary key, pdesc ) , sid number references student( ...

  7. 【Hibernate框架】关联映射(多对多关联映射)

    按着我们的总结行进计划,接下来,就是有关于多对多映射的总结了. 我们来举个例子啊,很长时间以来,房价暴涨不落,但是还有很多人拥有很多套房产,假如说,一个富豪拥有九套房产,家里人么准去住哪一套,我们就以 ...

  8. 【Hibernate框架】关联映射(一对多,多对一)

    根据我们的总结计划,上篇文章我们总结了有关于一对一映射相关知识,接下来,我们进行下一个阶段,一对多.多对一映射相关知识. 场景设定: 国家规定,一个人只能在一个公司上班,一个公司可以拥有很多员工.我们 ...

  9. hibernate笔记--单(双)向的多对多映射关系

    在讲单向的多对多的映射关系的案例时,我们假设我们有两张表,一张角色表Role,一张权限表Function,我们知道一个角色或者说一个用户,可能有多个操作权限,而一种操作权限同时被多个用户所拥有,假如我 ...

随机推荐

  1. thinkphp 快速缓存

    如果你的存储数据没有有效期的需求,那么系统还提供了一个快速缓存方法F可以用来更快的操作. 大理石平台厂家 F方法可以支持不同的存储类型,如果是文件类型的话,默认保存在DATA_PATH目录下面. 快速 ...

  2. Elasticsearch集群状态查看命令

    _cat $ curl localhost:9200/_cat=^.^=/_cat/allocation/_cat/shards/_cat/shards/{index}/_cat/master/_ca ...

  3. hive的数据压缩

    hive的数据压缩 在实际工作当中,hive当中处理的数据,一般都需要经过压缩,前期我们在学习hadoop的时候,已经配置过hadoop的压缩,我们这里的hive也是一样的可以使用压缩来节省我们的MR ...

  4. 一句话下载总结(用于后渗透上传payload)

    利用ftp来下载payload文件 echo open 192.168.1.1 21> ftp.txt echo ftp>> ftp.txt echo bin >> ft ...

  5. Java-Class-@I:org.springframework.web.bind.annotation.RequestMapping

    ylbtech-Java-Class-@I:org.springframework.web.bind.annotation.RequestMapping 1.返回顶部   2.返回顶部 1. pack ...

  6. webpack3

    6月20号webpack推出了3.0版本,官方也发布了公告.根据公告介绍,webpack团队将未来版本的改动聚焦在社区提出的功能需求,同时将保持一个快速.稳定的发布节奏.本文主要依据公告内容,简单介绍 ...

  7. Opencv稍微高级点的鼠标事件-OpenCV步步精深

    今天我们要来点稍微高级的东西.在我们按下鼠标时可以画矩形,而我们按下键盘m键时,切换到画圆的模式,再按下m键,回到画矩形模式. 一起来写下代码,首先当然还是调用库 import cv2 import ...

  8. 01、requests 基本使用

    requests模块的基本使用 基于网络请求的模块. 环境的安装:pip install requests 作用:模拟浏览器发起请求 分析requests的编码流程: 1.指定url 2.发起了请求 ...

  9. uoj21 【UR #1】缩进优化

    题目 题意简介明了,需要找到一个\(T\),最小化 \[\sum_{i=1}^n\left \lfloor \frac{a_i}{T} \right \rfloor+\sum_{i=1}^na_i\% ...

  10. 最短路(sp

    #include<stdio.h> #include<iostream> #include<queue> using namespace std; #define ...