Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

1.

Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

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>

Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

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章 映射实体关联关系-006双向多对多(分解为一对多)

上一篇:eclipse代码补全按键修改成Tab


下一篇:linux网络:常用命令(二)