Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1.
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="learnings" lazy="true" inverse="true" cascade="save-update"> 15 <key column="MONKEY_ID" /> 16 <one-to-many class="mypack.Learning" /> 17 </set> 18 19 </class> 20 21 </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 <set name="learnings" lazy="true" inverse="true" cascade="save-update"> 15 <key column="TEACHER_ID" /> 16 <one-to-many class="mypack.Learning" /> 17 </set> 18 19 </class> 20 </hibernate-mapping>
4.
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.Learning" table="LEARNING" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="gongfu" column="GONGFU" type="string" /> 13 14 <many-to-one name="monkey" column="MONKEY_ID" class="mypack.Monkey" not-null="true" /> 15 <many-to-one name="teacher" column="TEACHER_ID" class="mypack.Teacher" not-null="true" /> 16 17 18 </class> 19 </hibernate-mapping>
5.
1 package mypack; 2 import java.util.Set; 3 import java.util.HashSet; 4 import java.util.Iterator; 5 6 public class Monkey{ 7 8 9 private Long id; 10 private String name; 11 private Set learnings=new HashSet(); 12 13 public Monkey(String name, Set learnings) { 14 this.name = name; 15 this.learnings = learnings; 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 getLearnings() { 40 return this.learnings; 41 } 42 43 public void setLearnings(Set learnings) { 44 this.learnings = learnings; 45 } 46 47 }
6.
1 package mypack; 2 3 import java.util.Set; 4 import java.util.HashSet; 5 6 public class Teacher{ 7 private Long id; 8 private String name; 9 private Set learnings=new HashSet(); 10 11 /** full constructor */ 12 public Teacher(String name,Set learnings ) { 13 this.name = name; 14 this.learnings=learnings; 15 } 16 17 /** default constructor */ 18 public Teacher() { 19 } 20 21 public String getName() { 22 return this.name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public Long getId() { 30 return this.id; 31 } 32 33 public void setId(Long id) { 34 this.id = id; 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 46 }
7.
1 package mypack; 2 public class Learning{ 3 private Long id; 4 private Teacher teacher; 5 private Monkey monkey; 6 private String gongfu; 7 private int quantity; 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 Long getId() { 20 return this.id; 21 } 22 23 public void setId(Long id) { 24 this.id = id; 25 } 26 27 public String getGongfu() { 28 return this.gongfu; 29 } 30 31 public void setGongfu(String gongfu) { 32 this.gongfu = gongfu; 33 } 34 35 public Monkey getMonkey() { 36 return this.monkey; 37 } 38 39 public void setMonkey(Monkey monkey) { 40 this.monkey = monkey; 41 } 42 43 public Teacher getTeacher() { 44 return this.teacher; 45 } 46 47 public void setTeacher(Teacher teacher) { 48 this.teacher = teacher; 49 } 50 51 }
8.
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 Set learnings=monkey.getLearnings(); 59 Iterator it=learnings.iterator(); //初始化Learnings 60 while(it.hasNext()){ 61 Learning learning=(Learning)it.next(); 62 Hibernate.initialize(learning.getTeacher()); //初始化Teacher 63 } 64 tx.commit(); 65 return monkey; 66 67 }catch (RuntimeException e) { 68 if (tx != null) { 69 tx.rollback(); 70 } 71 throw e; 72 } finally { 73 session.close(); 74 } 75 } 76 77 public void printMonkey(Monkey monkey){ 78 System.out.println("名字:"+monkey.getName()); 79 80 Set learnings=monkey.getLearnings(); 81 Iterator it=learnings.iterator(); 82 while(it.hasNext()){ 83 Learning learning=(Learning)it.next(); 84 System.out.println("-----------------------"); 85 System.out.println("老师:"+learning.getTeacher().getName()); 86 System.out.println("功夫:"+learning.getGongfu()); 87 } 88 89 } 90 91 public void test(){ 92 93 Teacher teacher1=new Teacher("二郎神",null); 94 Teacher teacher2=new Teacher("红孩儿",null); 95 saveTeacher(teacher1); 96 saveTeacher(teacher2); 97 98 Monkey monkey=new Monkey(); 99 monkey.setName("智多星"); 100 Learning learning1=new Learning(teacher1,monkey,"七十三变"); 101 Learning learning2=new Learning(teacher2,monkey,"三昧真火"); 102 103 monkey.getLearnings().add(learning1); 104 monkey.getLearnings().add(learning2); 105 saveMonkey(monkey); 106 107 monkey=loadMonkey(monkey.getId()); 108 printMonkey(monkey); 109 110 } 111 112 public static void main(String args[]){ 113 new BusinessService().test(); 114 sessionFactory.close(); 115 } 116 }
9.
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 ID bigint not null, 19 MONKEY_ID bigint not null, 20 TEACHER_ID bigint not null, 21 GONGFU varchar(15), 22 primary key(ID) 23 ); 24 25 alter table LEARNING add index IDX_MONKEY(MONKEY_ID), 26 add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID); 27 28 alter table LEARNING add index IDX_TEACHER(TEACHER_ID), 29 add constraint FK_TEACHER foreign key (TEACHER_ID) references TEACHERS(ID);
10.
You can do anything you set your mind to, man!