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. </class>
  18. </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. private Long id;
  9. private String name;
  10. private Set learnings=new HashSet();
  11.  
  12. public Monkey(String name, Set learnings) {
  13. this.name = name;
  14. this.learnings = learnings;
  15. }
  16.  
  17. /** default constructor */
  18. public Monkey() {
  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. }

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

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

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.

Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)的更多相关文章

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

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  2. Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

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

    0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...

  4. Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  5. Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  6. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  7. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  8. Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  9. Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

随机推荐

  1. .Net中的Socket通讯

    .NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是: ·Socket类 这个低层的类用于管理连接,WebReq ...

  2. NOSQL之【redis的安全策略】

    原文:http://redis.io/topics/security 1.Redis的安全模式 可信环境下的可信用户才可访问redis.这意味着,将redis服务器直接暴露在Internet或者不可信 ...

  3. 如何修改 Discuz 门户文章页默认视频大小

    在 Discuz 系统中,论坛插入 Flash 等可以输入自定义的尺寸,但是门户文章页不可以修改.经过一番研究,找到了修改门户文章页默认视频大小的方法如下,希望对你有用:找到:/source/func ...

  4. 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog

    SweetAlertDialog(sweet-alert-dialog)是一个套制作精美.动画效果出色生动的Android对话.消息提示框 SweetAlertDialog(sweet-alert-d ...

  5. Configure Log Shipping

    准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...

  6. Java String.split()注意点

    //String[] aa = "aaa|bbb|ccc".split("|");//错误 String[] aa = "aaa|bbb|ccc&qu ...

  7. oracle-11g创建用户名的时候默认区分大小写

    oracle11g-11.2.0.3.0 - 64bit oracle-11g创建用户名的时候默认区分大小写 设置不区分大小写: alter system set sec_case_sensitive ...

  8. myeclipse配置下tomcat debug启动很无比慢

    myeclipse配置下tomcat debug启动很无比慢,而run启动很快今天照常使用MyEclipse 6.5 Blue Edition进行开发,但是却遇到一个怪问题.在MyEclipse环境下 ...

  9. error:LNK2005 已经在*.obj中定义

    为什么会出现这个错误??“error LNK2005: 已经在*.obj中定义”  编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误,弄清楚它形成的原 ...

  10. OC的@property 和 @synthesize id

    学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...