hibernate双向多对多关联

来源:互联网 发布:mac ansible 编辑:程序博客网 时间:2024/05/21 04:42

一.

注意:1.在持久化类里面写set的时候一定要初始化,否则会出现初始化问题

    2.在类关联文件中要设置<!-- inverse="true" 双向的时候一边加上就行,否则出现主键冲突-->


二.代码

1.AItem类

package cn.edu.sdut.hibernate.nton;import java.util.HashSet;import java.util.Set;public class AItem {private int aId;private String aName;<span style="color:#ff0000;">private Set<BItem> bSet = new HashSet<>();//注意为set初始化,否则会出现空指针异常</span>public int getaId() {return aId;}public void setaId(int aId) {this.aId = aId;}public String getaName() {return aName;}public void setaName(String aName) {this.aName = aName;}public Set<BItem> getbSet() {return bSet;}public void setbSet(Set<BItem> bSet) {this.bSet = bSet;}}
2.BItem类

package cn.edu.sdut.hibernate.nton;import java.util.HashSet;import java.util.Set;public class BItem {private int bId;private String bName;<span style="color:#ff0000;">private Set<AItem> aSet = new HashSet<>();//注意为set初始化,否则会出现空指针异常</span>public Set<AItem> getaSet() {return aSet;}public void setaSet(Set<AItem> aSet) {this.aSet = aSet;}public int getbId() {return bId;}public void setbId(int bId) {this.bId = bId;}public String getbName() {return bName;}public void setbName(String bName) {this.bName = bName;}}

3.AItem.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 2016-10-30 23:47:20 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="cn.edu.sdut.hibernate.nton.AItem" table="AITEM">        <id name="aId" type="int">            <column name="A_ID" />            <generator class="native" />        </id>        <property name="aName" type="java.lang.String" >            <column name="A_NAME" />        </property>               <span style="color:#ff0000;"> <!-- table: 指定中间表 -->        <set name="bSet" table="AITEM_BITEM"><!-- inverse="true" 双向的时候一边加上就行,否则出现主键冲突-->            <key>            <!-- 设置当前表在中间表的外键 -->                <column name="AID" />            </key>            <!-- 使用 many-to-many 指定多对多的关联关系. column 执行 bSet 集合中的持久化类在中间表的外键列的名称,也就是另一张表在中间表的外键  -->            <many-to-many class="cn.edu.sdut.hibernate.nton.BItem" column="BID"/>        </set></span>    </class></hibernate-mapping>

4.BItem.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 2016-10-30 23:47:20 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="cn.edu.sdut.hibernate.nton.BItem" table="BITEM">        <id name="bId" type="int" >            <column name="B_ID" />            <generator class="native" />        </id>        <property name="bName" type="java.lang.String" >            <column name="B_NAME" />        </property>        <!-- -->        <span style="color:#ff0000;"> <set name="aSet" table="AITEM_BITEM" inverse="true"><!-- inverse="true" 双向的时候一边加上就行,否则出现主键冲突-->            <key>            <!-- 设置当前表在中间表的外键 -->                <column name="BID" />            </key>            <!-- 使用 many-to-many 指定多对多的关联关系. column 执行 bSet 集合中的持久化类在中间表的外键列的名称,也就是另一张表在中间表的外键  -->            <many-to-many class="cn.edu.sdut.hibernate.nton.AItem" column="AID"/>        </set></span>             </class></hibernate-mapping>
5.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">csc</property>    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="connection.url">jdbc:mysql:///hibernate5</property>        <!-- 配置hibernate的基本信息 -->    <!-- hibernate 所使用的数据库方言 -->    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>        <!-- 执行sql时是否在控制台上打印 -->    <property name="show_sql">true</property>        <!-- 是否对sql进行格式化 -->    <property name="format_sql">true</property>        <!-- 指定自动生成数据表的策略 -->    <property name="hbm2ddl.auto">update</property>        <!-- 指定session的delete方法会把对象的id置为null -->    <property name="hibernate.use_identifier_rollback">true</property>        <!-- 指定数据库的隔离级别 -->    <property name="connection.isolation">2</property>        <!-- 配置c3p0数据池 -->    <property name="hibernate.c3p0.max_size">100</property>    <property name="hibernate.c3p0.min_size">20</property>    <property name="hibernate.c3p0.acquire_increment">5</property>    <property name="hibernate.c3p0.timeout">2000</property>    <property name="hibernate.c3p0.idle_test_period">2000</property>    <property name="hibernate.c3p0.max_statements">10</property>        <!-- 设定JDBC的statment读取数据的时候每次在数据库中读取的记录的条数 -->    <property name="hibernate.jdbc.fetch_size">100</property>    <!-- 设定对数据进行批量操作,批次更新和批次插入的批次大小 -->    <property name="hibernate.jdbc.batch_size">30</property>        <!-- 指定关联的xxx.hbm.xml文件 -->    <mapping resource="cn/edu/sdut/hibernate/helloworld/News.hbm.xml"/>    <mapping resource="cn/edu/sdut/hibernate/manytoone/Customer.hbm.xml"/>    <mapping resource="cn/edu/sdut/hibernate/manytoone/Order.hbm.xml"/>    <!--      <mapping resource="cn/edu/sdut/hibernate/onetoone/Department.hbm.xml"/>    <mapping resource="cn/edu/sdut/hibernate/onetoone/Manager.hbm.xml"/>        <mapping resource="cn/edu/sdut/hibernate/onetoone2/Department.hbm.xml"/>    <mapping resource="cn/edu/sdut/hibernate/onetoone2/Manager.hbm.xml"/>    -->    <mapping resource="cn/edu/sdut/hibernate/nton/AItem.hbm.xml"/>    <mapping resource="cn/edu/sdut/hibernate/nton/BItem.hbm.xml"/>    </session-factory></hibernate-configuration>

6.TestNtoN类

package cn.edu.sdut.hibernate.nton;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.After;import org.junit.Before;import org.junit.Test;public class TestNtoN {private SessionFactory sessionFactory;private Session session;private Transaction transaction;@Beforepublic void init(){Configuration configuration = new Configuration().configure();ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();sessionFactory = configuration.buildSessionFactory(serviceRegistry);session = sessionFactory.openSession();transaction = session.beginTransaction();}@Afterpublic void destory(){transaction.commit();session.close();sessionFactory.close();}@Testpublic void testSave(){AItem aItem = new AItem();aItem.setaName("AA1");AItem aItem2 = new AItem();aItem2.setaName("AA2");BItem bItem = new BItem();bItem.setbName("BB1");BItem bItem2 = new BItem();bItem2.setbName("BB2");aItem.getbSet().add(bItem);aItem.getbSet().add(bItem2);aItem2.getbSet().add(bItem);aItem2.getbSet().add(bItem2);bItem.getaSet().add(aItem);bItem.getaSet().add(aItem2);bItem2.getaSet().add(aItem);bItem2.getaSet().add(aItem2);session.save(aItem);session.save(aItem2);session.save(bItem);session.save(bItem2);}@Testpublic void testGet(){AItem aItem = (AItem) session.get(AItem.class, 1);System.out.println(aItem.getbSet());BItem bItem = (BItem) session.get(BItem.class, 1);System.out.println(bItem.getaSet().getClass().getName());}}




0 0