0.

1.

 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);

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.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>

3.

 <?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" /> </class>
</hibernate-mapping>

4.

 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;
} }

5.

 package mypack;
public class Teacher{
private Long id;
private String name; /** 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;
} }

6.

 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); Monkey monkey2=new Monkey();
monkey2.setName("ÀÏÍçͯ");
monkey2.getTeachers().add(teacher1); saveMonkey(monkey1);
saveMonkey(monkey2); monkey1=loadMonkey(monkey1.getId());
printMonkey(monkey1); } public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}

7.

Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多的更多相关文章

  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章 映射实体关联关系-004双向多对多(inverse="true")

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

  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逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

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

  7. Hibernate逍遥游记-第5章映射一对多-01单向<many-to-one>、cascade="save-update"、lazy、TransientObjectException

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

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

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

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

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

随机推荐

  1. php学习日志(4)-The mbstring extension is missing. Please check your PHP configuration错误及解决方法

    在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring extension is missing. Please che ...

  2. 《零成本实现Web自动化测试--基于Selenium》第一章 自动化测试基础

    第一篇 Selenium 和WebDriver工具篇 第一章 自动化测试基础 1.1    初识自动化测试 自动化测试有两种常见方式 1.1.1 代码驱动测试,又叫测试驱动开发(TDD) 1.1.2 ...

  3. .NET开发作业调度(job scheduling) - Quartz.NET

    Quartz.NET是JAVA Job Scheduling框架Quartz在.NET平台上的实现,可以满足小型乃至大型企业应用中的Job Scheduling. 通过Nuget安装Quartz.NE ...

  4. 1014. Waiting in Line (30)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  5. GOOGLE影像地图

    卫星地图高清 //  

  6. 深入PHP EOF(heredoc)用法详解

    介绍下使用EOF heredoc方式,输出长段内容的方法, <?php $name = '姓名'; print <<<EOT <html> <head> ...

  7. (菜鸟要飞系列)一,基于Asp.Net MVC5的后台管理系统(前言)

    今天真是个郁闷的日子,因为老师两个星期前给我的一个任务,用递归算法将Oracle数据库中用户信息及权限显示在jquery-treeView上,网上虽然有大神写出了这类算法,但是不贴全部代码,真的很难跟 ...

  8. Fibonacci数

    Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递 ...

  9. 活动 Activity 四种加载模式

    singleTop要求如果创建intent的时候栈顶已经有要创建的Activity的实例,则将intent发送给该实例,而不发送给新的实例.(注意是栈顶,不在栈顶照样创建新实例!) singleTas ...

  10. C#异常类总结

    http://msdn.microsoft.com/zh-cn/library/aa664610(v=vs.71).aspx C#异常类相关总结 C#异常类一.基类Exception C#异常类二.常 ...