1.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" column="NAME" type="string" /> <set name="teachers" table="LEARNING"
lazy="true"
cascade="save-update">
<key column="MONKEY_ID" />
<many-to-many class="mypack.Teacher" column="TEACHER_ID" />
</set> </class> </hibernate-mapping>

2.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Teacher" table="TEACHERS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" column="NAME" type="string" /> <set name="monkeys" table="LEARNING"
lazy="true"
inverse="true"
cascade="save-update">
<key column="TEACHER_ID" />
<many-to-many class="mypack.Monkey" column="MONKEY_ID" />
</set> </class>
</hibernate-mapping>

3.

 package mypack;

 import java.util.Set;
import java.util.HashSet; public class Monkey {
private Long id;
private String name;
private Set teachers=new HashSet(); public Monkey(String name, Set teachers) {
this.name = name;
this.teachers = teachers; } /** default constructor */
public Monkey() {
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Set getTeachers() {
return this.teachers;
} public void setTeachers(Set teachers) {
this.teachers = teachers;
} }

4.

 package mypack;
import java.util.Set;
import java.util.HashSet; public class Teacher {
private Long id;
private String name;
private Set monkeys=new HashSet(); /** full constructor */
public Teacher(String name) {
this.name = name;
} /** default constructor */
public Teacher() {
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public Set getMonkeys() {
return this.monkeys;
} public void setMonkeys(Set monkeys) {
this.monkeys = monkeys;
} }

5.

 package mypack;

 import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public Monkey loadMonkey(Long id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,id);
Hibernate.initialize(monkey.getTeachers());
tx.commit(); return monkey; }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey){
Set teachers=monkey.getTeachers();
Iterator it=teachers.iterator();
while(it.hasNext()){
Teacher teacher=(Teacher)it.next();
System.out.println(monkey.getName()+" "+teacher.getName());
} } public void test(){ Teacher teacher1=new Teacher("¶þÀÉÉñ");
Teacher teacher2=new Teacher("ºìº¢¶ù"); Monkey monkey1=new Monkey();
monkey1.setName("ÖǶàÐÇ");
monkey1.getTeachers().add(teacher1);
monkey1.getTeachers().add(teacher2);
teacher1.getMonkeys().add(monkey1);
teacher2.getMonkeys().add(monkey1); Monkey monkey2=new Monkey();
monkey2.setName("ÀÏÍçͯ");
monkey2.getTeachers().add(teacher1);
teacher1.getMonkeys().add(monkey2); saveMonkey(monkey1);
saveMonkey(monkey2); monkey1=loadMonkey(monkey1.getId());
printMonkey(monkey1); } public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}

6.

 drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; create table MONKEYS(
ID bigint not null,
NAME varchar(15),
primary key (ID)
); create table TEACHERS(
ID bigint not null,
NAME varchar(15),
primary key(ID)
); create table LEARNING(
MONKEY_ID bigint not null,
TEACHER_ID bigint not null,
primary key(MONKEY_ID,TEACHER_ID)
); alter table LEARNING add index IDX_MONKEY(MONKEY_ID),
add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID); alter table LEARNING add index IDX_TEACHER(TEACHER_ID),
add constraint FK_TEACHER foreign key (TEACHER_ID) references TEACHERS(ID);

Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")的更多相关文章

  1. Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  2. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  3. Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多

    0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...

  4. Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  5. Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  6. Hibernate逍遥游记-第5章映射一对多-02双向(<set>、<key>、<one-to-many>、inverse、cascade="all-delete-orphan")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  7. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  8. Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  9. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

随机推荐

  1. SequoiaDB数据库集群部署

    一般在多机环境下部署数据库的集群模式是比较繁琐的,下面我来分享一个如何通过shell脚本的方式简单.方便地部署我们的集群. 首先,我们要给机器配置信任关系,这样我们就无需手动的输入密码来执行ssh和s ...

  2. JVM学习---JAVA内存

    一.JAVA运行时数据区域:JAVA中的运行时内存区域有的随着虚拟机进程的启动而存在,有的区域则是依赖用户线程的启动和结束而建立和销毁的.包括以下的几个区域. 图. JAVA虚拟机运行时数据区 1.程 ...

  3. 基于jquery打造的网页右侧自动收缩浮动在线客服代码

    基于jquery打造的网页右侧自动收缩浮动在线QQ客服代码, 当前比较流行的一款QQ在线jquery特效代码, 代码中还带有IE6下PNG图片透明的特效,如果想研究IE6下PNG透明的同学也可以下载研 ...

  4. 查看leapmotion的frame信息

    leapmotion的SDK里有个c#实例,很详细,其中的frame类已经把这些封装的很完善了.可惜是控制台的,运行时很难去找那些动作对应的哪些数据.我今天做的就是把它们在窗体控件里分别显示出来,这样 ...

  5. 你的数据根本不够大,别老扯什么Hadoop了

    本文原名"Don't use Hadoop when your data isn't that big ",出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯 ...

  6. awk中文手册

    1. 前言 有关本手册 : 这是一本awk学习指引, 其重点着重于 : l        awk 适于解决哪些问题 ? l        awk 常见的解题模式为何 ? 为使读者快速掌握awk解题的模 ...

  7. Java中的main()方法详解

    在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...

  8. C++实现数字媒体二维图像变换

    C++实现数字媒体二维图像变换 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画正方形,画三角形的功能.并附带放大 ...

  9. C++中的快速排序(使用vector和数组的不同)

    1.快速排序是最最基本的排序算法之一,时间复杂度是O(nlog2(n)) 基本思想:分治法+递归 假设key为该序列的第一个元素,从后往前遍历,找到第一个小于key值的元素,将该元素赋值给左边的起始值 ...

  10. sqlserver 获取时间年月日时分秒

    转自:http://blog.itpub.net/14766526/viewspace-1156100/ select GETDATE() as '当前日期',DateName(year,GetDat ...