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

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

1.

 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   
15     <set name="learnings" lazy="true" table="LEARNING" >
16         <key column="MONKEY_ID" />
17         <composite-element class="mypack.Learning" >
18           <parent name="monkey" />
19           <many-to-one name="teacher" class="mypack.Teacher" column="TEACHER_ID" not-null="true"/>
20           <property name="gongfu" column="GONGFU" type="string" not-null="true" />
21          </composite-element> 
22     </set>
23      
24   </class>
25 
26 </hibernate-mapping>

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.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      <set name="learnings" lazy="true" inverse="true" table="LEARNING" >
15         <key column="TEACHER_ID" />
16         <composite-element class="mypack.Learning" >
17           <parent name="teacher" />
18           <many-to-one name="monkey" class="mypack.Monkey" column="MONKEY_ID" not-null="true"/>
19           <property name="gongfu" column="GONGFU" type="string" not-null="true" />
20          </composite-element> 
21     </set>
22  
23   </class>
24 </hibernate-mapping>

 

3.

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

 

4.

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

 

5.

 1 package mypack;
 2 
 3 public class Learning{
 4 
 5     private Teacher teacher;
 6     private Monkey monkey;
 7     private String gongfu;
 8 
 9     public Learning(Teacher teacher,Monkey monkey,String gongfu) {
10         this.teacher= teacher;
11         this.monkey = monkey;
12         this.gongfu=gongfu;
13    }
14 
15     /** default constructor */
16     public Learning() {
17     }
18 
19     public String getGongfu() {
20         return this.gongfu;
21     }
22 
23     public void setGongfu(String gongfu) {
24         this.gongfu = gongfu;
25     }
26 
27     public Monkey getMonkey() {
28         return this.monkey;
29     }
30 
31     public void setMonkey(Monkey monkey) {
32         this.monkey = monkey;
33     }
34 
35     public Teacher getTeacher() {
36         return this.teacher;
37     }
38 
39     public void setTeacher(Teacher teacher) {
40         this.teacher = teacher;
41     }
42     
43   }

 

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 void saveTeacher(Teacher teacher){
 36     Session session = sessionFactory.openSession();
 37     Transaction tx = null;
 38     try {
 39       tx = session.beginTransaction();
 40       session.save(teacher);
 41       tx.commit();
 42 
 43     }catch (RuntimeException e) {
 44       if (tx != null) {
 45         tx.rollback();
 46       }
 47       throw e;
 48     } finally {
 49       session.close();
 50     }
 51   }
 52  public Monkey loadMonkey(Long id){
 53     Session session = sessionFactory.openSession();
 54     Transaction tx = null;
 55     try {
 56       tx = session.beginTransaction();
 57       Monkey monkey=(Monkey)session.get(Monkey.class,id);
 58       
 59       Set learnings=monkey.getLearnings();
 60       Iterator it=learnings.iterator(); //初始化Learnings
 61       while(it.hasNext()){
 62         Learning learning=(Learning)it.next();
 63         Hibernate.initialize(learning.getTeacher()); //初始化Teacher
 64       }
 65       tx.commit();
 66 
 67       return monkey;
 68 
 69     }catch (RuntimeException e) {
 70       if (tx != null) {
 71         tx.rollback();
 72       }
 73       throw e;
 74     } finally {
 75       session.close();
 76     }
 77   }
 78 
 79   public void printMonkey(Monkey monkey){
 80     System.out.println("名字:"+monkey.getName());
 81 
 82     Set learnings=monkey.getLearnings();
 83     Iterator it=learnings.iterator();
 84     while(it.hasNext()){
 85       Learning learning=(Learning)it.next();
 86       System.out.println("-----------------------");
 87       System.out.println("老师:"+learning.getTeacher().getName());
 88       System.out.println("功夫:"+learning.getGongfu());
 89     }
 90 
 91   }
 92 
 93   public void test(){
 94 
 95       Teacher teacher1=new Teacher("二郎神",null);
 96       Teacher teacher2=new Teacher("红孩儿",null);
 97       saveTeacher(teacher1);
 98       saveTeacher(teacher2);
 99 
100       Monkey monkey=new Monkey();
101       monkey.setName("智多星");
102       Learning learning1=new Learning(teacher1,monkey,"七十三变");
103       Learning learning2=new Learning(teacher2,monkey,"三昧真火");
104       
105       monkey.getLearnings().add(learning1);
106       monkey.getLearnings().add(learning2);
107       saveMonkey(monkey);
108 
109       monkey=loadMonkey(monkey.getId());
110       printMonkey(monkey);
111 
112   }
113 
114   public static void main(String args[]){
115     new BusinessService().test();
116     sessionFactory.close();
117   }
118 }

 

7.

 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   GONGFU varchar(15),
21   primary key(MONKEY_ID,TEACHER_ID,GONGFU)
22 );
23 
24 alter table LEARNING add index IDX_MONKEY(MONKEY_ID), 
25 add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);
26 
27 alter table LEARNING add index IDX_TEACHER(TEACHER_ID), 
28 add constraint FK_TEACHER foreign key (TEACHER_ID) references TEACHERS(ID);

 

8.

你可能感兴趣的:(Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\\))