Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1.
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="learnings" lazy="true" inverse="true" cascade="save-update">
<key column="MONKEY_ID" />
<one-to-many class="mypack.Learning" />
</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" /> <set name="learnings" lazy="true" inverse="true" cascade="save-update">
<key column="TEACHER_ID" />
<one-to-many class="mypack.Learning" />
</set> </class>
</hibernate-mapping>
4.
<?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.Learning" table="LEARNING" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="gongfu" column="GONGFU" type="string" /> <many-to-one name="monkey" column="MONKEY_ID" class="mypack.Monkey" not-null="true" />
<many-to-one name="teacher" column="TEACHER_ID" class="mypack.Teacher" not-null="true" /> </class>
</hibernate-mapping>
5.
package mypack;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator; public class Monkey{ private Long id;
private String name;
private Set learnings=new HashSet(); public Monkey(String name, Set learnings) {
this.name = name;
this.learnings = learnings;
} /** 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 getLearnings() {
return this.learnings;
} public void setLearnings(Set learnings) {
this.learnings = learnings;
} }
6.
package mypack; import java.util.Set;
import java.util.HashSet; public class Teacher{
private Long id;
private String name;
private Set learnings=new HashSet(); /** full constructor */
public Teacher(String name,Set learnings ) {
this.name = name;
this.learnings=learnings;
} /** 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 getLearnings() {
return this.learnings;
} public void setLearnings(Set learnings) {
this.learnings = learnings;
} }
7.
package mypack;
public class Learning{
private Long id;
private Teacher teacher;
private Monkey monkey;
private String gongfu;
private int quantity; public Learning(Teacher teacher,Monkey monkey,String gongfu) {
this.teacher= teacher;
this.monkey = monkey;
this.gongfu=gongfu;
} /** default constructor */
public Learning() {
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getGongfu() {
return this.gongfu;
} public void setGongfu(String gongfu) {
this.gongfu = gongfu;
} public Monkey getMonkey() {
return this.monkey;
} public void setMonkey(Monkey monkey) {
this.monkey = monkey;
} public Teacher getTeacher() {
return this.teacher;
} public void setTeacher(Teacher teacher) {
this.teacher = teacher;
} }
8.
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 void saveTeacher(Teacher teacher){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(teacher);
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);
Set learnings=monkey.getLearnings();
Iterator it=learnings.iterator(); //初始化Learnings
while(it.hasNext()){
Learning learning=(Learning)it.next();
Hibernate.initialize(learning.getTeacher()); //初始化Teacher
}
tx.commit();
return monkey; }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey){
System.out.println("名字:"+monkey.getName()); Set learnings=monkey.getLearnings();
Iterator it=learnings.iterator();
while(it.hasNext()){
Learning learning=(Learning)it.next();
System.out.println("-----------------------");
System.out.println("老师:"+learning.getTeacher().getName());
System.out.println("功夫:"+learning.getGongfu());
} } public void test(){ Teacher teacher1=new Teacher("二郎神",null);
Teacher teacher2=new Teacher("红孩儿",null);
saveTeacher(teacher1);
saveTeacher(teacher2); Monkey monkey=new Monkey();
monkey.setName("智多星");
Learning learning1=new Learning(teacher1,monkey,"七十三变");
Learning learning2=new Learning(teacher2,monkey,"三昧真火"); monkey.getLearnings().add(learning1);
monkey.getLearnings().add(learning2);
saveMonkey(monkey); monkey=loadMonkey(monkey.getId());
printMonkey(monkey); } public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}
9.
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(
ID bigint not null,
MONKEY_ID bigint not null,
TEACHER_ID bigint not null,
GONGFU varchar(15),
primary key(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);
10.
Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)的更多相关文章
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- SQLite简易入门
本文内容来源:https://www.dataquest.io/mission/129/introduction-to-sql 本文所用数据来源:https://github.com/fivethir ...
- linux文件的通用操作方法学习
2014-07-29 23:36:10 在linux下用文件描述符来表示设备文件和普通文件.文件描述符是一个整型的数据,所有对文件的操作都通过文件描述符实现. 文件描述符示文件系统中连接用户空间和内核 ...
- Makefile之wildcard
1.wildcard : 扩展通配符2.notdir : 去除路径3.patsubst :替换通配符 例子:建立一个测试目录,在测试目录下建立一个名为sub的子目录$ mkdir test$ cd t ...
- 编辑器未包含main类型解决方法
将文件移到 src 这个 Java Source Folder 下面去,现在在外面的 java 文件不会被当成一个需要编译的类,eclipse 不会编译 Java Source Folder 外面的任 ...
- mysql获取日期(将时间戳转换成短日期格式)
且看如下: '; 结果: +-------------------------------------+---------------------+ | date_format(create_time ...
- android 播放语音文件出现 prepare failed ,不能下载amr文件
amr文件的路径正确,但是android 却不能播放出来. 调试发现时根本就没有下载下来 原因: IIS服务器不允许下载该文件,需要配置MIME 解决方法: 进入IIS目录,配置MIME
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...
- 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- PAT-乙级-1046. 划拳(15)
1046. 划拳(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 划拳是古老中国酒文化的一个有趣的组成部分 ...
- mysqlbinlog工具基于日志恢复详细解释
如果每天都会生成大量的二进制日志,这些日志长时间不清理的话,将会对磁盘空间带来很大的浪费,所以定期清理日志是DBA维护mysql的一个重要工作 1)RESET MASTER在上面查看日志存放的文件夹中 ...