Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

时间:2021-05-23 22:51:34

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="teachers" table="LEARNING"
lazy="true"
cascade="save-update">
<key column="MONKEY_ID" />
<many-to-many class="mypack.Teacher" column="TEACHER_ID" />
</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="monkeys" table="LEARNING"
lazy="true"
inverse="true"
cascade="save-update">
<key column="TEACHER_ID" />
<many-to-many class="mypack.Monkey" column="MONKEY_ID" />
</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 teachers=new HashSet(); public Monkey(String name, Set teachers) {
this.name = name;
this.teachers = teachers; } /** 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 getTeachers() {
return this.teachers;
} public void setTeachers(Set teachers) {
this.teachers = teachers;
} }

4.

 package mypack;
import java.util.Set;
import java.util.HashSet; public class Teacher {
private Long id;
private String name;
private Set monkeys=new HashSet(); /** full constructor */
public Teacher(String name) {
this.name = name;
} /** 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 getMonkeys() {
return this.monkeys;
} public void setMonkeys(Set monkeys) {
this.monkeys = monkeys;
} }

5.

 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 Monkey loadMonkey(Long id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,id);
Hibernate.initialize(monkey.getTeachers());
tx.commit(); return monkey; }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey){
Set teachers=monkey.getTeachers();
Iterator it=teachers.iterator();
while(it.hasNext()){
Teacher teacher=(Teacher)it.next();
System.out.println(monkey.getName()+" "+teacher.getName());
} } public void test(){ Teacher teacher1=new Teacher("¶þÀÉÉñ");
Teacher teacher2=new Teacher("ºìº¢¶ù"); Monkey monkey1=new Monkey();
monkey1.setName("ÖǶàÐÇ");
monkey1.getTeachers().add(teacher1);
monkey1.getTeachers().add(teacher2);
teacher1.getMonkeys().add(monkey1);
teacher2.getMonkeys().add(monkey1); Monkey monkey2=new Monkey();
monkey2.setName("ÀÏÍçͯ");
monkey2.getTeachers().add(teacher1);
teacher1.getMonkeys().add(monkey2); saveMonkey(monkey1);
saveMonkey(monkey2); monkey1=loadMonkey(monkey1.getId());
printMonkey(monkey1); } public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}

6.

 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,
primary key(MONKEY_ID,TEACHER_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);