博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
双向多对多
阅读量:5239 次
发布时间:2019-06-14

本文共 4334 字,大约阅读时间需要 14 分钟。

                                    建立多对多双向关联关系                                                   

            1.创建Employee持久化类

   public class Employee implements java.io.Serializable {

    // Fields

    private Integer empid;

    private String empname;

    private Set<Project> projects = new HashSet<Project>();

    ..........

}

===================================================

     2.创建Project持久化类

public class Project implements java.io.Serializable {

    private Integer proid;

    private String proname;

private Set<Employee> employees = new HashSet<Employee>();

...............

}

===================================================

3.创建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>

    <class name="cn.happy.daymanymany.Employee" table="EMPLOYEE">

        <id name="empid" type="java.lang.Integer">

            <column name="EMPID"/>

            <generator class="sequence">

              <param name="sequence">SEQ_ID</param>

            </generator>

        </id>

        <property name="empname" type="java.lang.String">

            <column name="EMPNAME" length="32" not-null="true" />

        </property>

        <set name="projects" inverse="true" table="PROEMP">

            <key column="REMPID"/><!-- 表PROEMP的外键REMPID -->

            <many-to-many class="cn.happy.daymanymany.Project" column="RPROID" />

        </set>

    </class>

</hibernate-mapping>

======================================================================

4.创建Project.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>

    <class name="cn.happy.daymanymany.Project" table="PROJECT">

        <id name="proid" type="java.lang.Integer">

            <column name="PROID"/>

            <generator class="sequence">

              <param name="sequence">SEQ_ID</param>

            </generator>

        </id>

        <property name="proname" type="java.lang.String">

            <column name="PRONAME" length="32" not-null="true" />

        </property>

        <set name="employees" table="PROEMP" cascade="save-update">

            <key column="RPROID" />

            <many-to-many class="cn.happy.daymanymany.Employee" column="REMPID" />

        </set>

    </class>

</hibernate-mapping>

===================================================================

 05.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 name="foo">

     

        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>

        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:HR</property>

        <property name="connection.username">happy</property>

        <property name="connection.password">happy</property>

   

        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="show_sql">true</property>

        <property name="format_sql">true</property>

        <property name="hbm2ddl.auto">update</property>

       

        <mapping resource="cn/happy/daymanymany/Project.hbm.xml"/>

        <mapping resource="cn/happy/daymanymany/Employee.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

================================================================

6.书写测试类和方法

package cn.happy.test;

import java.util.List;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.junit.Test;

import cn.happy.daymanymany.Employee;

import cn.happy.daymanymany.Project;

 

public class Day06OneManyTest {

    // 创建session工厂

    public static SessionFactory factory;

    // 创建Session对象

    public static Session session;

    // 通过代码块赋值

    static {

        factory = new Configuration().configure().buildSessionFactory();

        session = factory.openSession();

    }

    @Test

    public void addTest(){

        Transaction tran = null;

        try {

            tran = session.beginTransaction();

            //创建EMP对象

            Employee emp=new Employee();

            emp.setEmpname("李小龙");

            //创建Pro对象

            Project pro=new Project();

            pro.setProname("海淀花园");

            //指定工程需要的员工

            pro.getEmployees().add(emp);

            //指定员工所属的工程

            emp.getProjects().add(pro);

            session.save(pro);

            // 事务提交

            tran.commit();

            System.out.println("成功");

        } catch (Exception e) {

            e.printStackTrace();

            if (tran != null) {

                tran.rollback();

            }

            throw new RuntimeException("错误");

        } finally {

            // 关闭session

            session.close();

        }

    }

}

=========================================================================

转载于:https://www.cnblogs.com/ainiaiwo/p/5837263.html

你可能感兴趣的文章
卷积中的参数
查看>>
Linux中Zabbix4.0的搭建
查看>>
《LoadRunner没有告诉你的》之六——获取有效的性能需求
查看>>
51nod1076 (边双连通)
查看>>
Item 9: Avoid Conversion Operators in Your APIs(Effective C#)
查看>>
js去除空格
查看>>
学习Spring Boot:(二十八)Spring Security 权限认证
查看>>
IT学习神器——慕课网App获App Store、Android应用市场重磅推荐
查看>>
Linux网络状态工具ss命令使用详解
查看>>
致妈妈
查看>>
面向对象封装与继承
查看>>
bzoj 1217: [HNOI2003]消防局的设立
查看>>
数据存储 mysql
查看>>
Django 表单
查看>>
配置phpmemcache扩展,Loaded Configuration File (none)
查看>>
阿里云服务器ubuntu64位配置java_web环境
查看>>
Android 自定义组合控件
查看>>
第二章 搭建Android开发环境--读书笔记
查看>>
代理的理解
查看>>
editPlus,3.7V 注册码
查看>>