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

0.

1.

 1 drop database if exists SAMPLEDB;
 2 create database SAMPLEDB;
 3 use SAMPLEDB;
 4 
 5 create table MONKEYS(
 6    ID bigint not null,
 7    NAME varchar(15),
 8    primary key (ID)
 9 );
10 
11 create table TEACHERS(
12   ID bigint not null,
13   NAME varchar(15),
14   primary key(ID)
15 );
16 
17 create table LEARNING(
18   MONKEY_ID bigint not null,
19   TEACHER_ID bigint not null,
20   primary key(MONKEY_ID,TEACHER_ID)
21 );
22 
23 alter table LEARNING add index IDX_MONKEY(MONKEY_ID), 
24 add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);
25 
26 alter table LEARNING add index IDX_TEACHER(TEACHER_ID), 
27 add constraint FK_TEACHER foreign key (TEACHER_ID) references TEACHERS(ID);

2.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Monkey" table="MONKEYS" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="name" column="NAME" type="string" />
13        
14     <set name="teachers" table="LEARNING"
15         lazy="true"
16         cascade="save-update">
17         <key column="MONKEY_ID" />
18         <many-to-many class="mypack.Teacher" column="TEACHER_ID" />
19     </set>
20        
21   </class>
22 
23 </hibernate-mapping>

3.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Teacher" table="TEACHERS" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="name" column="NAME" type="string" />
13      
14   </class>
15 </hibernate-mapping>

4.

 1 package mypack;
 2 import java.util.Set;
 3 import java.util.HashSet;
 4 
 5 public class Monkey {
 6 
 7 
 8     private Long id;
 9     private String name;
10     private Set teachers=new HashSet();
11  
12     public Monkey(String name, Set teachers) {
13         this.name = name;
14         this.teachers = teachers;
15        
16     }
17 
18     /** default constructor */
19     public Monkey() {
20     }
21 
22 
23     public Long getId() {
24         return this.id;
25     }
26 
27     public void setId(Long id) {
28         this.id = id;
29     }
30 
31     public String getName() {
32         return this.name;
33     }
34 
35     public void setName(String name) {
36         this.name = name;
37     }
38 
39     public Set getTeachers() {
40         return this.teachers;
41     }
42 
43     public void setTeachers(Set teachers) {
44         this.teachers = teachers;
45     }
46 
47  }

5.

 1 package mypack;
 2 public class Teacher{
 3     private Long id;
 4     private String name;
 5 
 6     /** full constructor */
 7     public Teacher(String name ) {
 8         this.name = name;
 9     }
10 
11     /** default constructor */
12     public Teacher() {
13     }
14 
15     public String getName() {
16         return this.name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public Long getId() {
24         return this.id;
25     }
26 
27     public void setId(Long id) {
28         this.id = id;
29     }
30    
31 }

6.

 1 package mypack;
 2 
 3 import org.hibernate.*;
 4 import org.hibernate.cfg.Configuration;
 5 import java.util.*;
 6 
 7 public class BusinessService{
 8   public static SessionFactory sessionFactory;
 9   static{
10      try{
11        Configuration config = new Configuration().configure();
12        sessionFactory = config.buildSessionFactory();
13     }catch(RuntimeException e){e.printStackTrace();throw e;}
14   }
15 
16   
17   public void saveMonkey(Monkey monkey){
18     Session session = sessionFactory.openSession();
19     Transaction tx = null;
20     try {
21       tx = session.beginTransaction();
22       session.save(monkey);
23       tx.commit();
24 
25     }catch (RuntimeException e) {
26       if (tx != null) {
27         tx.rollback();
28       }
29       throw e;
30     } finally {
31       session.close();
32     }
33   }
34 
35  public Monkey loadMonkey(Long id){
36     Session session = sessionFactory.openSession();
37     Transaction tx = null;
38     try {
39       tx = session.beginTransaction();
40       Monkey monkey=(Monkey)session.get(Monkey.class,id);
41       Hibernate.initialize(monkey.getTeachers());
42       tx.commit();
43       
44       return monkey;
45 
46     }catch (RuntimeException e) {
47       if (tx != null) {
48         tx.rollback();
49       }
50       throw e;
51     } finally {
52       session.close();
53     }
54   }
55    
56   public void printMonkey(Monkey monkey){
57     Set teachers=monkey.getTeachers();
58     Iterator it=teachers.iterator();
59     while(it.hasNext()){
60       Teacher teacher=(Teacher)it.next();
61       System.out.println(monkey.getName()+" "+teacher.getName());
62     }
63      
64   }
65 
66  public void test(){
67      
68       Teacher teacher1=new Teacher("¶þÀÉÉñ");
69       Teacher teacher2=new Teacher("ºìº¢¶ù");
70 
71       Monkey monkey1=new Monkey();
72       monkey1.setName("ÖǶàÐÇ");
73       monkey1.getTeachers().add(teacher1);
74       monkey1.getTeachers().add(teacher2);
75 
76       Monkey monkey2=new Monkey();
77       monkey2.setName("ÀÏÍçͯ");
78       monkey2.getTeachers().add(teacher1);
79 
80       saveMonkey(monkey1);
81       saveMonkey(monkey2);
82 
83       monkey1=loadMonkey(monkey1.getId());
84       printMonkey(monkey1);
85       
86   }
87 
88   public static void main(String args[]){
89     new BusinessService().test();
90     sessionFactory.close();
91   }
92 }

 

7.

原文地址:https://www.cnblogs.com/shamgod/p/5299811.html