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 ...
随机推荐
- DevExpress12.2.6 安装顺序记录
环境DelphiXE,实测DevExpress手工安装顺序: 1.ExpressCore Library 2.XP Theme Manager 3.ExpressGDI+ Library 4.Expr ...
- pandas库学习笔记(二)DataFrame入门学习
Pandas基本介绍——DataFrame入门学习 前篇文章中,小生初步介绍pandas库中的Series结构的创建与运算,今天小生继续“死磕自己”为大家介绍pandas库的另一种最为常见的数据结构D ...
- gcc链接程序时出现undefined reference to""错误
如:: undefined reference to ‘mq_unlink',意思是指函数mq_unlink没有定义. 可以使用如下步骤找到该函数所在的库: 1).查找哪些库包含了或使用了该函数:gr ...
- -sh: ./helloworld: not found
最近在玩FriendlyARM mini2440的板子,编译了一个helloworld,通过ftp上传到开发版的文件系统中,chmod 777 helloworld,运行./helloworld,出现 ...
- Seafile V4.1 安装笔记
yum -y install gcc gcc-c++ make cmake pcre pcre-devel expat expat-devel curl wget mlocate gd gd-deve ...
- Java中构造函数执行顺序的问题
1, 先执行内部静态对象的构造函数,如果有多个按定义的先后顺序执行:而且静态类的构造函数只会被执行一次,只在其第一个对象创建时调用,即便是创建了同一个类的多个对象,例如main()函数里b1,b2创 ...
- tcp-client-c++
#include "stdafx.h" #include <Winsock2.h> #include <iostream> #pragma comment( ...
- 帝国cms刷洗内容页提示.phome_ecms_news_data_' doesn't exist
帝国cms后台刷新提示.phome_ecms_news_data_' doesn't exist解决方法: 刷新所有信息内容页面时提示“Table '*.phome_ecms_article_data ...
- 二分查找or折半查找
package com.gxf.search; /** * 测试折半查找or二分查找 * @author xiangfei * */ public class BiSearch { /** * 非递归 ...
- Sublime Text 2 入门
SublimeText 2 的介绍视频: http://player.youku.com/player.php/partnerid/XOTcy/sid/XMzU5NzQ5ODgw/v.swf 以下 ...