• Hibernate的多对多映射


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

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7     
     8         <!-- 配置连接数据库的基本信息 -->
     9         <property name="connection.username">root</property>
    10         <property name="connection.password"></property>
    11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    12         <!-- <property name="connection.url">jdbc:mysql:///mis</property> -->
    13         <property name="connection.url">
    14             <![CDATA[jdbc:mysql://localhost:3306/mis?useUnicode=true&characterEncoding=utf8]]>
    15         </property>
    16 
    17 
    18         <!-- 配置 hibernate 的基本信息 -->
    19         <!-- hibernate 所使用的数据库方言 -->
    20         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    21 
    22         <!-- 执行操作时是否在控制台打印 SQL -->
    23         <property name="show_sql">true</property>
    24 
    25         <!-- 是否对 SQL 进行格式化 -->
    26         <property name="format_sql">true</property>
    27 
    28         <!-- 指定自动生成数据表的策略 -->
    29         <property name="hbm2ddl.auto">update</property>
    30         
    31         <!-- 设置 Hibernate 的事务隔离级别 2:读已提交的记录-->
    32         <property name="connection.isolation">2</property>
    33         
    34         <!-- 删除对象后, 使其 OID 置为 null -->
    35         <property name="use_identifier_rollback">true</property>
    36         
    37         <!-- 配置 C3P0 数据源 -->
    38         <property name="hibernate.c3p0.max_size">10</property>
    39         <property name="hibernate.c3p0.min_size">5</property>
    40         <property name="c3p0.acquire_increment">2</property>
    41         
    42         <property name="c3p0.idle_test_period">2000</property>
    43         <property name="c3p0.timeout">2000</property>
    44         
    45         <property name="c3p0.max_statements">10</property>
    46         
    47         <!-- 设定 JDBC 的 Statement 读取数据的时候每次从数据库中取出的记录条数 -->
    48         <property name="hibernate.jdbc.fetch_size">100</property>
    49         
    50         <!-- 设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
    51         <property name="jdbc.batch_size">30</property>
    52 
    53         <!-- 指定关联的 .hbm.xml 文件 -->    
    54         <!--  
    55         <mapping resource="com/mcs/hibernate/entities/onetoone/foreign/Manager.hbm.xml" />
    56         <mapping resource="com/mcs/hibernate/entities/onetoone/foreign/Department.hbm.xml" />
    57           -->
    58         <!--   
    59         <mapping resource="com/mcs/hibernate/entities/onetoone/primary/Manager.hbm.xml" />
    60         <mapping resource="com/mcs/hibernate/entities/onetoone/primary/Department.hbm.xml" />
    61          -->
    62         <!-- 
    63         <mapping resource="com/mcs/hibernate/entities/manytomany/Category.hbm.xml" />
    64         <mapping resource="com/mcs/hibernate/entities/manytomany/Product.hbm.xml" />
    65          -->
    66         <!--  
    67         <mapping resource="com/mcs/hibernate/entities/subclass/Person.hbm.xml" />
    68          -->
    69         <!--  
    70         <mapping resource="com/mcs/hibernate/entities/joined/subclass/Person.hbm.xml" />
    71          -->
    72         
    73         <mapping resource="com/mcs/hibernate/entities/union/subclass/Person.hbm.xml" />
    74         
    75 
    76     </session-factory>
    77 
    78 </hibernate-configuration>
    View Code

    二、建立多对多的映射

    1、创建Java实体类

     1 package com.mcs.hibernate.entities.manytomany;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 public class Category {
     7     
     8     private Integer id;
     9     private String name;
    10     
    11     private Set<Product> products = new HashSet<>();
    12 
    13     public Integer getId() {
    14         return id;
    15     }
    16 
    17     public void setId(Integer id) {
    18         this.id = id;
    19     }
    20 
    21     public String getName() {
    22         return name;
    23     }
    24 
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28 
    29     public Set<Product> getProducts() {
    30         return products;
    31     }
    32 
    33     public void setProducts(Set<Product> products) {
    34         this.products = products;
    35     }
    36     
    37     
    38 
    39 }
    View Code
     1 package com.mcs.hibernate.entities.manytomany;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 public class Product {
     7     
     8     private Integer id;
     9     private String name;
    10     
    11     private Set<Category> categories = new HashSet<>();
    12     
    13     public Integer getId() {
    14         return id;
    15     }
    16     public void setId(Integer id) {
    17         this.id = id;
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25     public Set<Category> getCategories() {
    26         return categories;
    27     }
    28     public void setCategories(Set<Category> categories) {
    29         this.categories = categories;
    30     }
    31     
    32     
    33 
    34 }
    View Code

    2、根据实体类创建对应的 hbm.xml文件

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- Generated 2015-10-28 14:53:27 by Hibernate Tools 3.5.0.Final -->
     5 <hibernate-mapping>
     6     <class name="com.mcs.hibernate.entities.manytomany.Category" table="CATEGORIES">
     7         <id name="id" type="java.lang.Integer">
     8             <column name="ID" />
     9             <generator class="native" />
    10         </id>
    11         <property name="name" type="java.lang.String">
    12             <column name="NAME" />
    13         </property>
    14         
    15         <!-- table: 指定中间表 -->
    16         <set name="products" table="CATEGORIES_PRODUCTS" inverse="true">
    17             <key>
    18                 <column name="CATEGORY_ID" />
    19             </key>
    20             <!-- 使用 many-to-many 指定多对多的关联关系. column 执行 Set 集合中的持久化类在中间表的外键列的名称  -->
    21             <many-to-many class="com.mcs.hibernate.entities.manytomany.Product" column="PRODUCT_ID"/>
    22         </set>
    23     </class>
    24 </hibernate-mapping>
    View Code
     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- Generated 2015-10-28 14:53:27 by Hibernate Tools 3.5.0.Final -->
     5 <hibernate-mapping>
     6     <class name="com.mcs.hibernate.entities.manytomany.Product" table="PRODUCTS">
     7         <id name="id" type="java.lang.Integer">
     8             <column name="ID" />
     9             <generator class="native" />
    10         </id>
    11         <property name="name" type="java.lang.String">
    12             <column name="NAME" />
    13         </property>
    14         
    15         <!-- table: 指定中间表 -->
    16         <set name="categories" table="CATEGORIES_PRODUCTS">
    17             <key>
    18                 <column name="PRODUCT_ID" />
    19             </key>
    20             <!-- 使用 many-to-many 指定多对多的关联关系. column 执行 Set 集合中的持久化类在中间表的外键列的名称  -->
    21             <many-to-many class="com.mcs.hibernate.entities.manytomany.Category" column="CATEGORY_ID"/>
    22         </set>
    23     </class>
    24 </hibernate-mapping>
    View Code

    3、备注:

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

        table: 指定中间表

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

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

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

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

  • 相关阅读:
    HPU第二次个人训练
    2019CCPC江西省赛
    CodeForces-913C 派对柠檬水
    [Codeforces Round #737 (Div. 2)] C Moamen and XOR (T1 D1
    E-Tree Xor_2021牛客暑期多校训练营4
    Educational Codeforces Round 107 (Rated for Div. 2) E Colorings and Dominoes
    状压dp 练习
    权值线段树模板(自用)
    Planar Reflections
    Codeforces Round #688 (Div. 2) D Checkpoints
  • 原文地址:https://www.cnblogs.com/maocs/p/4920100.html
Copyright © 2020-2023  润新知