Hibernate中双向多对多的两种配置方式

1.建立多对多双向关联关系

 package cn.happy.entitys;

 import java.util.HashSet;
import java.util.Set; public class Employee {
private Integer empid;
private String empname;
private Set<ProEmp> emps = new HashSet<ProEmp>(); public Set<ProEmp> getEmps() {
return emps;
} public void setEmps(Set<ProEmp> emps) {
this.emps = emps;
} public Integer getEmpid() {
return empid;
} public void setEmpid(Integer empid) {
this.empid = empid;
} public String getEmpname() {
return empname;
} public void setEmpname(String empname) {
this.empname = empname;
} public Set<ProEmp> getProjects() {
return projects;
} public void setProjects(Set<ProEmp> projects) {
this.projects = projects;
} private Set<ProEmp> projects = new HashSet<ProEmp>();
}

Employee.java

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entitys"> <class name="Employee" table="EMPLOYEE">
<id name="empid" column="EMPID">
<generator class="sequence">
<param name="sequence">STU_SID</param>
</generator>
</id>
<property name="empname" type="string" column="EMPNAME"></property> <set name="projects" table="PROEMP" cascade="save-update">
<key column="REMPID"></key>
<many-to-many class="Project" column="RPROID"></many-to-many>
</set> </class> </hibernate-mapping>

Employee.hbm.xml

 package cn.happy.entitys;

 import java.util.Set;

 public class Project {
private Integer proid;
private String proname;
private Set<ProEmp> pros;
public Integer getProid() {
return proid;
}
public Set<ProEmp> getPros() {
return pros;
}
public void setPros(Set<ProEmp> pros) {
this.pros = pros;
}
public void setProid(Integer proid) {
this.proid = proid;
}
public String getProname() {
return proname;
}
public void setProname(String proname) {
this.proname = proname;
}
}

Project.java

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entity"> <class name="Project" table="PROJECT">
<id name="proid" column="PROID">
<generator class="sequence">
<param name="sequence">STU_SID</param>
</generator>
</id>
<property name="proname" type="string" column="PRONAME"></property>
<set name="employeses" table="PROEMP" cascade="save-update">
<key column="RPROID"></key>
<many-to-many class="Employee" column="REMPID"></many-to-many>
</set>
</class> </hibernate-mapping>

Project.hbm.xml

中间关系表

 package cn.happy.entitys;

 public class ProEmp {
private Integer id;
private Project pro;
private Employee emp; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Project getPro() {
return pro;
} public void setPro(Project pro) {
this.pro = pro;
} public Employee getEmp() {
return emp;
} public void setEmp(Employee emp) {
this.emp = emp;
}
}

ProEmp.java

 <?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>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">hmy</property>
<property name="connection.password">hmy</property> <!-- SQL dialect SQL方言-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout 控制台显示-->
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- 是否格式化 -->
<property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property> <!-- <mapping resource="cn/happy/entity/Student.hbm.xml" /> -->
<!-- <mapping resource="cn/happy/entity/grade.hbm.xml" /> -->
<!-- <mapping class="cn.happy.entity.Grade"/> -->
<!-- <mapping resource="cn/happy/entity/Dept.hbm.xml" />
<mapping resource="cn/happy/entity/Emp.hbm.xml" /> -->
<mapping resource="cn/happy/entity/Employee.hbm.xml" />
<mapping resource="cn/happy/entity/Project.hbm.xml" /> <!-- <mapping resource="cn/happy/entitys/Employee.hbm.xml" />
<mapping resource="cn/happy/entitys/Project.hbm.xml" />
<mapping resource="cn/happy/entitys/ProEmp.hbm.xml" /> --> </session-factory> </hibernate-configuration>

hibernate.hbm.xml

 package cn.happy.ui;

 import org.hibernate.Session;
import org.hibernate.Transaction; import cn.happy.entity.Employee;
import cn.happy.entity.Project;
import cn.happy.util.HibernateUtil; public class Test3 {
public static void main(String[] args) {
select();
} /**
* 检索
* */
public static void select(){
Session session = HibernateUtil.currentSession();
Project pro = (Project)session.load(Project.class, 1);
for (Employee item : pro.getEmployeses()) {
System.out.println("员工:"+item.getEmpname()+"\t参加的项目:");
for (Project project : item.getProjects()) {
System.out.println(project.getProname());
}
}
HibernateUtil.closeSession();
} /**
* 添加
* */
public static void one(){
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction(); Project pro=new Project();
pro.setProname("ADO"); Employee emp=new Employee();
emp.setEmpname("范子阳");
Employee emp2=new Employee();
emp2.setEmpname("李夏鹏"); pro.getEmployeses().add(emp);
pro.getEmployeses().add(emp2); //未声明级联
//emp.getProjects().add(pro);
//emp2.getProjects().add(pro); session.save(pro); tx.commit();
HibernateUtil.closeSession();
}
}

测试类

2.建立多对多双向关联 拆成两个一对多

 package cn.happy.entitys;

 import java.util.HashSet;
import java.util.Set; public class Employee {
private Integer empid;
private String empname;
private Set<ProEmp> emps = new HashSet<ProEmp>(); public Set<ProEmp> getEmps() {
return emps;
} public void setEmps(Set<ProEmp> emps) {
this.emps = emps;
} public Integer getEmpid() {
return empid;
} public void setEmpid(Integer empid) {
this.empid = empid;
} public String getEmpname() {
return empname;
} public void setEmpname(String empname) {
this.empname = empname;
} public Set<ProEmp> getProjects() {
return projects;
} public void setProjects(Set<ProEmp> projects) {
this.projects = projects;
} private Set<ProEmp> projects = new HashSet<ProEmp>();
}

Employee.java

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entitys"> <class name="Employee" table="EMPLOYEE">
<id name="empid" column="EMPID">
<generator class="sequence">
<param name="sequence">STU_SID</param>
</generator>
</id> </class> </hibernate-mapping>

Employee.hbm.xml

 package cn.happy.entitys;

 import java.util.Set;

 public class Project {
private Integer proid;
private String proname;
private Set<ProEmp> pros;//为中间关系表所对应的实体类的类型
public Integer getProid() {
return proid;
}
public Set<ProEmp> getPros() {
return pros;
}
public void setPros(Set<ProEmp> pros) {
this.pros = pros;
}
public void setProid(Integer proid) {
this.proid = proid;
}
public String getProname() {
return proname;
}
public void setProname(String proname) {
this.proname = proname;
}
}

Project.java

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entitys"> <class name="Project" table="PROJECT">
<id name="proid" column="PROID">
<generator class="sequence">
<param name="sequence">STU_SID</param>
</generator>
</id>
<property name="proname" type="string" column="PRONAME"></property> </class> </hibernate-mapping>

Project.hbm.xml

 package cn.happy.entitys;

 public class ProEmp {
private Integer id;
private Project pro;
private Employee emp; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Project getPro() {
return pro;
} public void setPro(Project pro) {
this.pro = pro;
} public Employee getEmp() {
return emp;
} public void setEmp(Employee emp) {
this.emp = emp;
}
}

ProEmp.java

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entitys"> <class name="ProEmp" table="PROEMP">
<id name="id" column="ID">
<generator class="sequence">
<param name="sequence">STU_SID</param>
</generator>
</id> <many-to-one name="emp" class="Employee" column="empid"></many-to-one>
<many-to-one name="pro" class="Project" column="proid"></many-to-one>
</class> </hibernate-mapping>

ProEmp.hbm.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>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">hmy</property>
<property name="connection.password">hmy</property> <!-- SQL dialect SQL方言-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout 控制台显示-->
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- 是否格式化 -->
<property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property> <!-- <mapping resource="cn/happy/entity/Student.hbm.xml" /> -->
<!-- <mapping resource="cn/happy/entity/grade.hbm.xml" /> -->
<!-- <mapping class="cn.happy.entity.Grade"/> -->
<!-- <mapping resource="cn/happy/entity/Dept.hbm.xml" />
<mapping resource="cn/happy/entity/Emp.hbm.xml" /> -->
<!-- <mapping resource="cn/happy/entity/Employee.hbm.xml" />
<mapping resource="cn/happy/entity/Project.hbm.xml" /> --> <mapping resource="cn/happy/entitys/Employee.hbm.xml" />
<mapping resource="cn/happy/entitys/Project.hbm.xml" />
<mapping resource="cn/happy/entitys/ProEmp.hbm.xml" /> </session-factory> </hibernate-configuration>

Hinernate.hbm.xml

 package cn.happy.ui;

 import org.hibernate.Session;
import org.hibernate.Transaction; import cn.happy.entitys.Employee;
import cn.happy.entitys.ProEmp;
import cn.happy.entitys.Project;
import cn.happy.util.HibernateUtil; public class Test4 {
public static void main(String[] args) {
test();
}
public static void test(){
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction(); Project pro=new Project();
pro.setProname("ADO"); Employee emp=new Employee();
emp.setEmpname("范子阳"); ProEmp proemp=new ProEmp();
proemp.setEmp(emp);
proemp.setPro(pro); session.save(proemp);
session.save(emp);
session.save(pro); tx.commit();
HibernateUtil.closeSession();
} }

测试类

Hibernate中双向多对多的两种配置方式的更多相关文章

  1. hibernate 一对一 one to one的两种配置方式

    hibernate中one-to-one两种配置方式 标签: hibernateHibernateone-to-one 2013-02-19 17:44 11445人阅读 评论(1) 收藏 举报  分 ...

  2. IOC容器在web容器中初始化——(一)两种配置方式

    参考文章http://blog.csdn.net/liuganggao/article/details/44083817,http://blog.csdn.net/u013185616/article ...

  3. Ajax中的get和post两种请求方式的异同

    Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别.   1. get是把参数数据队列加到提交表单的A ...

  4. MyBatis中主键回填的两种实现方式

    主键回填其实是一个非常常见的需求,特别是在数据添加的过程中,我们经常需要添加完数据之后,需要获取刚刚添加的数据 id,无论是 Jdbc 还是各种各样的数据库框架都对此提供了相关的支持,本文我就来和和大 ...

  5. 转:Ajax中的get和post两种请求方式的异同

    1. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML ...

  6. C#中迭代器的概念和两种实现方式

    1.首先我们看下IEnumerable接口定义:   namespace System.Collections    {        // Summary:        //     Expose ...

  7. 在k8s中部署前后端分离项目进行访问的两种配置方式

    第一种方式 (1) nginx配置中只写前端项目的/根路径配置 前端项目使用的Dockerfile文件内容 把前端项目编译后生成的dist文件夹放在nginx的html默认目录下,浏览器访问前端项目时 ...

  8. 第四节:框架前期准备篇之进程外Session的两种配置方式

    一. 基本介绍 1. 背景:Asp.Net默认的Session机制是进程内,存储在服务器端内存中,有这么几个缺点: ①:既然存在内存中,空间有限,不能存储大数据量信息,数据量多的话Session会被挤 ...

  9. springboot之jackson的两种配置方式

    springboot 针对jackson是自动化配置的,如果需要修改,有两种方式: 方式一:通过application.yml 配置属性说明:## spring.jackson.date-format ...

随机推荐

  1. 【Hadoop/Hive/mapreduce】系列之如何删除HIVE 表格的分区

    今天的一个业务场景就是要把三年的数据从第一天不停的融合起来,每一天作为表格一个新的分区.由于空间有限,数据量很大,可能每天数据都是几十个G的大小.所以我需要做的一点就是在融合这一天之后,删除一天的分区 ...

  2. IAR 编译时找不到头文件的解决方法

    Fatal Error[Pe1696]: cannot open source file "x.h" 那是因为头文件路径没有找对 到报错的.c源文件 选中右键 选择options ...

  3. 如果文件里是汉字的话,这地方seek括号里面只能是偶数

    >>> f=open("E:/pythonLearn/140.txt") >>> f.seek(8) #如果文件里是汉字的话,这地方seek括号 ...

  4. react+redux状态管理实现排序 合并多个reducer文件

    这个demo只有一个reducer 所以合并reducer这个demo用不到 ,但是我写出来这样大家以后可以用到,很好用,管理多个reducer,因为只要用到redux就不会只有一个reducer所以 ...

  5. 18 Django-组件拾遗

    一 Django的form组件 forms组件 二 Django的model form组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的 ...

  6. P2183 巧克力(二分答案)

    P2183 巧克力 题目描述 佳佳邀请了M个同学到家里玩.为了招待客人,她需要将巧克力分给她的好朋友们.她有N(1<=N<=5000)块巧克力,但是大小各不一样,第i块巧克力大小为为1*X ...

  7. svn git 导入本地文件到远程服务器 import

    以前,想要把本地的一个文件上传到svn 或者git 服务器的时候,都要先把服务器上的文件夹down 下来,然后把要添加的文件添加进去,然后提交. 想想都麻烦. 现在我们用import 命令就可以做到, ...

  8. erlang中的原子(atom)内部实现[转]

    转自: http://www.kongqingquan.com/archives/208#more-208 Erlang中的atom由通用hash表实现,虚拟机中atom最终的用数值表示,对应表中的下 ...

  9. CentOS7配置图形界面及设置vnc远程连接、windows远程桌面连接

    安装CentOS桌面 yum groupinstall "GNOME Desktop" 重启,进入终端,将启动模式变更为图形模式 systemctl set-default gra ...

  10. mongodb安装和配置三步走

    最近在重新学习node,所以和同事一起搞了个模仿新浪微博的项目,项目刚开始,所以其他的东西就暂时先不提.这里介绍下mongodb的安装.直接搜索可以看到很多介绍,但是我第一次是失败了,不过看了好几个还 ...