Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
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="learnings" lazy="true" table="LEARNING" >
<key column="MONKEY_ID" />
<composite-element class="mypack.Learning" >
<parent name="monkey" />
<many-to-one name="teacher" class="mypack.Teacher" column="TEACHER_ID" not-null="true"/>
<property name="gongfu" column="GONGFU" type="string" not-null="true" />
</composite-element>
</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="learnings" lazy="true" inverse="true" table="LEARNING" >
<key column="TEACHER_ID" />
<composite-element class="mypack.Learning" >
<parent name="teacher" />
<many-to-one name="monkey" class="mypack.Monkey" column="MONKEY_ID" not-null="true"/>
<property name="gongfu" column="GONGFU" type="string" not-null="true" />
</composite-element>
</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 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;
} }
4.
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;
} }
5.
package mypack; public class Learning{ private Teacher teacher;
private Monkey monkey;
private String gongfu; public Learning(Teacher teacher,Monkey monkey,String gongfu) {
this.teacher= teacher;
this.monkey = monkey;
this.gongfu=gongfu;
} /** default constructor */
public Learning() {
} 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;
} }
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 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();
}
}
7.
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,
GONGFU varchar(15),
primary key(MONKEY_ID,TEACHER_ID,GONGFU)
); 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);
8.
Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)的更多相关文章
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- 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逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第5章映射一对多-02双向(<set>、<key>、<one-to-many>、inverse、cascade="all-delete-orphan")
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 ...
随机推荐
- hbase meta表的结构
下面看下hbase:meta 表的结构,hbase:meta表中,保存了每个表的region地址,还有一些其他信息,例如region的名字,HRegionInfo,服务器的信息.hbase:meta表 ...
- NetworkInfo 手机中的网络类型
04-27 21:56:54.442: E/NetworkInfo(26457): NetworkInfo: type: mobile[EDGE], state: DISCONNECTED/IDLE, ...
- DML,DDL,DCL,DQL的区别
DML 英文缩写 DML = Data Manipulation Language,数据操纵语言,命令使用户能够查询数据库以及操作已有数据库中的数据的计算机语言.具体是指是UPDATE更新.INS ...
- MySQL语法语句大全
一.SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件(如表.索引.字段等)的建立和操纵. 以下是一些重要的SQL快速参考,有关SQ ...
- Linq to Entities
首先要添加一个ADO.NET实体数据模型 添加一个Entities 对象,其用法和linqtosql类似例如: StudentInfoEntities2 entity = new StudentInf ...
- cocos2.2.3中创建精灵对象的三大类方法
1.众生相,皆精灵 2.精灵的类继承关系 class CCSprite : public CCNode, public CCNodeRGBA, public CCTextureProtocol 3.创 ...
- aes 解密出现 java.lang.NumberFormatException: Invalid int: "ch"
原因: 将加密/解密的seed 和 加密内容顺序放反. decrypt(String seed, String encrypted) 附上AES解密/加密代码(android开发): package ...
- C#异常类总结
http://msdn.microsoft.com/zh-cn/library/aa664610(v=vs.71).aspx C#异常类相关总结 C#异常类一.基类Exception C#异常类二.常 ...
- 3529: [Sdoi2014]数表 - BZOJ
Description 有一张N×m的数表,其第i行第j列(1 < =i < =n,1 < =j < =m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a ...
- mysql数据恢复
[1] 当数据库被删除后的恢复方法 首先建立一个测试用的数据库. mysql -u root -p123123 ← 用root登录到MySQL服务器 Enter password: ← ...