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

时间:2022-11-15 22:52:24

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>

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

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.