Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
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 <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>
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="monkeys" table="LEARNING" 15 lazy="true" 16 inverse="true" 17 cascade="save-update"> 18 <key column="TEACHER_ID" /> 19 <many-to-many class="mypack.Monkey" column="MONKEY_ID" /> 20 </set> 21 22 </class> 23 </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 teachers=new HashSet(); 10 11 public Monkey(String name, Set teachers) { 12 this.name = name; 13 this.teachers = teachers; 14 15 } 16 17 /** default constructor */ 18 public Monkey() { 19 } 20 21 22 public Long getId() { 23 return this.id; 24 } 25 26 public void setId(Long id) { 27 this.id = id; 28 } 29 30 public String getName() { 31 return this.name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public Set getTeachers() { 39 return this.teachers; 40 } 41 42 public void setTeachers(Set teachers) { 43 this.teachers = teachers; 44 } 45 46 }
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 monkeys=new HashSet(); 9 10 /** full constructor */ 11 public Teacher(String name) { 12 this.name = name; 13 } 14 15 /** default constructor */ 16 public Teacher() { 17 } 18 19 public String getName() { 20 return this.name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public Long getId() { 28 return this.id; 29 } 30 31 public void setId(Long id) { 32 this.id = id; 33 } 34 35 public Set getMonkeys() { 36 return this.monkeys; 37 } 38 39 public void setMonkeys(Set monkeys) { 40 this.monkeys = monkeys; 41 } 42 43 44 }
5.
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 teacher1.getMonkeys().add(monkey1); 76 teacher2.getMonkeys().add(monkey1); 77 78 Monkey monkey2=new Monkey(); 79 monkey2.setName("ÀÏÍçͯ"); 80 monkey2.getTeachers().add(teacher1); 81 teacher1.getMonkeys().add(monkey2); 82 83 saveMonkey(monkey1); 84 saveMonkey(monkey2); 85 86 monkey1=loadMonkey(monkey1.getId()); 87 printMonkey(monkey1); 88 89 } 90 91 public static void main(String args[]){ 92 new BusinessService().test(); 93 sessionFactory.close(); 94 } 95 }
6.
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);
You can do anything you set your mind to, man!