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. <discriminator column="MONKEY_TYPE" type="string" />
  12. <property name="name" type="string" column="NAME" />
  13.  
  14. <many-to-one
  15. name="team"
  16. column="TEAM_ID"
  17. class="mypack.Team"
  18. />
  19.  
  20. <subclass name="mypack.JMonkey" discriminator-value="JM" >
  21. <property name="color" column="COLOR" type="string" />
  22. </subclass>
  23.  
  24. <subclass name="mypack.CMonkey" discriminator-value="CM" >
  25. <property name="length" column="LENGTH" type="double" />
  26. </subclass>
  27.  
  28. </class>
  29.  
  30. </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.Team" table="TEAMS" >
  8. <id name="id" type="long" column="ID">
  9. <generator class="increment"/>
  10. </id>
  11.  
  12. <property name="name" type="string" column="NAME" />
  13. <set
  14. name="monkeys"
  15. inverse="true"
  16. >
  17. <key column="TEAM_ID" />
  18. <one-to-many class="mypack.Monkey" />
  19. </set>
  20.  
  21. </class>
  22. </hibernate-mapping>

4.

  1. package mypack;
  2.  
  3. abstract public class Monkey{
  4.  
  5. private Long id;
  6. private String name;
  7. private Team team;
  8.  
  9. /** full constructor */
  10. public Monkey(String name,Team team) {
  11. this.name = name;
  12. this.team = team;
  13. }
  14.  
  15. /** default constructor */
  16. public Monkey() {
  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 getName() {
  28. return this.name;
  29. }
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public Team getTeam() {
  36. return this.team;
  37. }
  38.  
  39. public void setTeam(Team team) {
  40. this.team = team;
  41. }
  42. }

5.

  1. package mypack;
  2.  
  3. public class CMonkey extends Monkey {
  4.  
  5. private double length;
  6.  
  7. /** full constructor */
  8. public CMonkey(String name, double length,Team team) {
  9. super(name,team);
  10. this.length=length;
  11.  
  12. }
  13.  
  14. /** default constructor */
  15. public CMonkey() {
  16. }
  17.  
  18. public double getLength() {
  19. return this.length;
  20. }
  21.  
  22. public void setLength(double length) {
  23. this.length = length;
  24. }
  25. }

6.

  1. package mypack;
  2.  
  3. public class JMonkey extends Monkey{
  4.  
  5. private String color;
  6.  
  7. /** full constructor */
  8. public JMonkey(String name, String color,Team team) {
  9. super(name,team);
  10. this.color=color;
  11. }
  12.  
  13. /** default constructor */
  14. public JMonkey() {
  15. }
  16.  
  17. public String getColor() {
  18. return this.color;
  19. }
  20.  
  21. public void setColor(String color) {
  22. this.color = color;
  23. }
  24.  
  25. }

7.

  1. package mypack;
  2.  
  3. import java.util.Set;
  4. import java.util.HashSet;
  5.  
  6. public class Team {
  7.  
  8. private Long id;
  9. private String name;
  10. private Set monkeys=new HashSet();
  11.  
  12. /** full constructor */
  13. public Team(String name, Set monkeys) {
  14. this.name = name;
  15. this.monkeys = monkeys;
  16. }
  17.  
  18. /** default constructor */
  19. public Team() {
  20. }
  21.  
  22. /** minimal constructor */
  23. public Team(Set monkeys) {
  24. this.monkeys = monkeys;
  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 String getName() {
  36. return this.name;
  37. }
  38.  
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42.  
  43. public Set getMonkeys() {
  44. return this.monkeys;
  45. }
  46.  
  47. public void setMonkeys(Set monkeys) {
  48. this.monkeys = monkeys;
  49. }
  50. }

8.

  1. package mypack;
  2.  
  3. import org.hibernate.*;
  4. import org.hibernate.cfg.Configuration;
  5. import java.util.*;
  6. import java.sql.*;
  7.  
  8. public class BusinessService{
  9. public static SessionFactory sessionFactory;
  10. static{
  11. try{
  12. Configuration config = new Configuration().configure();
  13. sessionFactory = config.buildSessionFactory();
  14. }catch(RuntimeException e){e.printStackTrace();throw e;}
  15.  
  16. }
  17.  
  18. public void saveMonkey(Monkey monkey) {
  19. Session session = sessionFactory.openSession();
  20. Transaction tx = null;
  21. List results=new ArrayList();
  22. try {
  23. tx = session.beginTransaction();
  24. session.save(monkey);
  25. tx.commit();
  26. }catch (RuntimeException e) {
  27. if (tx != null) {
  28. tx.rollback();
  29. }
  30. throw e;
  31. } finally {
  32. session.close();
  33. }
  34. }
  35.  
  36. public List findAllJMonkeys(){
  37. Session session = sessionFactory.openSession();
  38. Transaction tx = null;
  39.  
  40. try {
  41.  
  42. tx = session.beginTransaction();
  43. List results=session.createQuery("from JMonkey").list();
  44. tx.commit();
  45. return results;
  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 List findAllMonkeys(){
  57. Session session = sessionFactory.openSession();
  58. Transaction tx = null;
  59.  
  60. try {
  61. tx = session.beginTransaction();
  62. List results=session.createQuery("from Monkey").list();
  63. tx.commit();
  64. return results;
  65. }catch (RuntimeException e) {
  66. if (tx != null) {
  67. tx.rollback();
  68. }
  69. throw e;
  70. } finally {
  71. session.close();
  72. }
  73. }
  74.  
  75. public Team loadTeam(long id){
  76. Session session = sessionFactory.openSession();
  77. Transaction tx = null;
  78. try {
  79. tx = session.beginTransaction();
  80. Team team=(Team)session.get(Team.class,new Long(id));
  81. Hibernate.initialize(team.getMonkeys());
  82. tx.commit();
  83. return team;
  84. }catch (RuntimeException e) {
  85. if (tx != null) {
  86. tx.rollback();
  87. }
  88. throw e;
  89. } finally {
  90. session.close();
  91. }
  92. }
  93.  
  94. public void test(){
  95. List jMonkeys=findAllJMonkeys();
  96. printAllMonkeys(jMonkeys.iterator());
  97.  
  98. List monkeys=findAllMonkeys();
  99. printAllMonkeys(monkeys.iterator());
  100.  
  101. Team team=loadTeam(1);
  102. printAllMonkeys(team.getMonkeys().iterator());
  103.  
  104. Monkey monkey=new JMonkey("Mary","yellow",team);
  105. saveMonkey(monkey);
  106.  
  107. }
  108.  
  109. private void printAllMonkeys(Iterator it){
  110. while(it.hasNext()){
  111. Monkey m=(Monkey)it.next();
  112. if(m instanceof JMonkey)
  113. System.out.println(((JMonkey)m).getColor());
  114. else
  115. System.out.println(((CMonkey)m).getLength());
  116. }
  117. }
  118. public static void main(String args[]) {
  119. new BusinessService().test();
  120. sessionFactory.close();
  121. }
  122. }

9.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE hibernate-configuration
  3. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7. <session-factory>
  8. <property name="dialect">
  9. org.hibernate.dialect.MySQLDialect
  10. </property>
  11. <property name="connection.driver_class">
  12. com.mysql.jdbc.Driver
  13. </property>
  14. <property name="connection.url">
  15. jdbc:mysql://localhost:3306/sampledb
  16. </property>
  17. <property name="connection.username">
  18. root
  19. </property>
  20. <property name="connection.password">
  21. 1234
  22. </property>
  23.  
  24. <property name="show_sql">true</property>
  25.  
  26. <mapping resource="mypack/Team.hbm.xml" />
  27. <mapping resource="mypack/Monkey.hbm.xml" />
  28. </session-factory>
  29. </hibernate-configuration>

10.

  1. drop database if exists SAMPLEDB;
  2. create database SAMPLEDB;
  3. use SAMPLEDB;
  4.  
  5. create table TEAMS (
  6. ID bigint not null,
  7. NAME varchar(15),
  8. primary key (ID)
  9. );
  10. create table MONKEYS (
  11. ID bigint not null,
  12. NAME varchar(15),
  13. MONKEY_TYPE varchar(2),
  14. COLOR varchar(15),
  15. LENGTH double precision,
  16. TEAM_ID bigint,
  17. primary key (ID)
  18. );
  19.  
  20. alter table MONKEYS add index IDX_TEAM(TEAM_ID), add constraint FK_TEAM foreign key (TEAM_ID) references TEAMS (ID);
  21.  
  22. insert into TEAMS(ID,NAME) values(1,'ABC Company');
  23.  
  24. insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(1,'JM','Tom','yellow',null,1);
  25.  
  26. insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(2,'JM','Mike','orange',null,1);
  27.  
  28. insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(3,'CM','Jack',null,1.2,1);
  29.  
  30. insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(4,'CM','Linda',null,2.0,1);

Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)的更多相关文章

  1. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

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

  2. Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表

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

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

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

  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章 映射实体关联关系-006双向多对多(分解为一对多)

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

  6. Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)

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

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

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

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

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

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

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

随机推荐

  1. js 数组去重复键

    Array.prototype.deleteEle = function() { var newArr = this; for (var i = newArr.length - 1; i >= ...

  2. Oracle 表的连接方式(2)-----HASH JOIN的基本机制1

    我们对hash join的常见误解,一般包括两个: 第一个误解:是我们经常以为hash join需要对两个做join的表都做全表扫描 第二个误解:是经常以为hash join会选择比较小的表做buil ...

  3. openerp模块收藏 基于Lodop的报表打印模块(转载)

    基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...

  4. Log Parser 2.2

    Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 A ...

  5. JMS概述

    [1.面向消息的中间件]顾名思义,面向消息的中间件就是通过使用消息(而不是命令)将企业内的组件连接起来的系统.例如库存系统可能会与工资和会计系统进行通信,如果使用面向消息的中间件将他们连接在一起,就可 ...

  6. Asp.Net生命周期系列五

    如果您看了我的前四篇文章,应该知道目前Http请求已经流到了HttpModule这个程序员手中了,而且我们可以注册自己的HttpModule并且可以在里面注册一些事件来控制这个Http请求,但是到目前 ...

  7. DB天气app冲刺二阶段第八天

    今天突然感觉应该做收尾工作了 因为马上就要考试了,时间一下子就不够用了.. 今天主要修复了一下bug,然后天气基本能够实时准确了,就是多功能按钮还是没有弄好 准备简化一下功能. 明天看看还有什么需要改 ...

  8. 图片轮播插件-carouFredSel

    carouFredSel图片轮播插件基于Jquery,比较常规的轮播插件,支持滚轮及键盘左右按键,加入其它插件可实现更加复杂的特效. 主页地址:http://caroufredsel.dev7stud ...

  9. mysql存储过程 OUT or INOUT argument 3 for routine

    mysql存储过程出现: OUT or INOUT argument 3 for routine gotask.UserLogin is not a variable or NEW pseudo-va ...

  10. 百度地图API使用

    1.引用js脚本 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&am ...