您的位置:首页 > 其它

Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多

2016-03-20 21:49 579 查看
0.





1.



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);


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="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>




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" />

</class>
</hibernate-mapping>


4.

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;
}

}


5.

package mypack;
public class Teacher{
private Long id;
private String name;

/** 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;
}

}


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 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);

Monkey monkey2=new Monkey();
monkey2.setName("ÀÏÍçͯ");
monkey2.getTeachers().add(teacher1);

saveMonkey(monkey1);
saveMonkey(monkey2);

monkey1=loadMonkey(monkey1.getId());
printMonkey(monkey1);

}

public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}




7.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: