Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
1.
2.
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping
- PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping >
- <class name="mypack.Monkey" table="MONKEYS">
- <id name="id" type="long" column="ID">
- <generator class="increment"/>
- </id>
- <discriminator column="MONKEY_TYPE" type="string" />
- <property name="name" type="string" column="NAME" />
- <many-to-one
- name="team"
- column="TEAM_ID"
- class="mypack.Team"
- />
- <subclass name="mypack.JMonkey" discriminator-value="JM" >
- <property name="color" column="COLOR" type="string" />
- </subclass>
- <subclass name="mypack.CMonkey" discriminator-value="CM" >
- <property name="length" column="LENGTH" type="double" />
- </subclass>
- </class>
- </hibernate-mapping>
3.
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping
- PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping >
- <class name="mypack.Team" table="TEAMS" >
- <id name="id" type="long" column="ID">
- <generator class="increment"/>
- </id>
- <property name="name" type="string" column="NAME" />
- <set
- name="monkeys"
- inverse="true"
- >
- <key column="TEAM_ID" />
- <one-to-many class="mypack.Monkey" />
- </set>
- </class>
- </hibernate-mapping>
4.
- package mypack;
- abstract public class Monkey{
- private Long id;
- private String name;
- private Team team;
- /** full constructor */
- public Monkey(String name,Team team) {
- this.name = name;
- this.team = team;
- }
- /** default constructor */
- public Monkey() {
- }
- public Long getId() {
- return this.id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Team getTeam() {
- return this.team;
- }
- public void setTeam(Team team) {
- this.team = team;
- }
- }
5.
- package mypack;
- public class CMonkey extends Monkey {
- private double length;
- /** full constructor */
- public CMonkey(String name, double length,Team team) {
- super(name,team);
- this.length=length;
- }
- /** default constructor */
- public CMonkey() {
- }
- public double getLength() {
- return this.length;
- }
- public void setLength(double length) {
- this.length = length;
- }
- }
6.
- package mypack;
- public class JMonkey extends Monkey{
- private String color;
- /** full constructor */
- public JMonkey(String name, String color,Team team) {
- super(name,team);
- this.color=color;
- }
- /** default constructor */
- public JMonkey() {
- }
- public String getColor() {
- return this.color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- }
7.
- package mypack;
- import java.util.Set;
- import java.util.HashSet;
- public class Team {
- private Long id;
- private String name;
- private Set monkeys=new HashSet();
- /** full constructor */
- public Team(String name, Set monkeys) {
- this.name = name;
- this.monkeys = monkeys;
- }
- /** default constructor */
- public Team() {
- }
- /** minimal constructor */
- public Team(Set monkeys) {
- this.monkeys = monkeys;
- }
- public Long getId() {
- return this.id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set getMonkeys() {
- return this.monkeys;
- }
- public void setMonkeys(Set monkeys) {
- this.monkeys = monkeys;
- }
- }
8.
- package mypack;
- import org.hibernate.*;
- import org.hibernate.cfg.Configuration;
- import java.util.*;
- import java.sql.*;
- public class BusinessService{
- public static SessionFactory sessionFactory;
- static{
- try{
- Configuration config = new Configuration().configure();
- sessionFactory = config.buildSessionFactory();
- }catch(RuntimeException e){e.printStackTrace();throw e;}
- }
- public void saveMonkey(Monkey monkey) {
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- List results=new ArrayList();
- try {
- tx = session.beginTransaction();
- session.save(monkey);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public List findAllJMonkeys(){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- List results=session.createQuery("from JMonkey").list();
- tx.commit();
- return results;
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public List findAllMonkeys(){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- List results=session.createQuery("from Monkey").list();
- tx.commit();
- return results;
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public Team loadTeam(long id){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Team team=(Team)session.get(Team.class,new Long(id));
- Hibernate.initialize(team.getMonkeys());
- tx.commit();
- return team;
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void test(){
- List jMonkeys=findAllJMonkeys();
- printAllMonkeys(jMonkeys.iterator());
- List monkeys=findAllMonkeys();
- printAllMonkeys(monkeys.iterator());
- Team team=loadTeam(1);
- printAllMonkeys(team.getMonkeys().iterator());
- Monkey monkey=new JMonkey("Mary","yellow",team);
- saveMonkey(monkey);
- }
- private void printAllMonkeys(Iterator it){
- while(it.hasNext()){
- Monkey m=(Monkey)it.next();
- if(m instanceof JMonkey)
- System.out.println(((JMonkey)m).getColor());
- else
- System.out.println(((CMonkey)m).getLength());
- }
- }
- public static void main(String args[]) {
- new BusinessService().test();
- sessionFactory.close();
- }
- }
9.
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE hibernate-configuration
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="connection.url">
- jdbc:mysql://localhost:3306/sampledb
- </property>
- <property name="connection.username">
- root
- </property>
- <property name="connection.password">
- 1234
- </property>
- <property name="show_sql">true</property>
- <mapping resource="mypack/Team.hbm.xml" />
- <mapping resource="mypack/Monkey.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
10.
- drop database if exists SAMPLEDB;
- create database SAMPLEDB;
- use SAMPLEDB;
- create table TEAMS (
- ID bigint not null,
- NAME varchar(15),
- primary key (ID)
- );
- create table MONKEYS (
- ID bigint not null,
- NAME varchar(15),
- MONKEY_TYPE varchar(2),
- COLOR varchar(15),
- LENGTH double precision,
- TEAM_ID bigint,
- primary key (ID)
- );
- alter table MONKEYS add index IDX_TEAM(TEAM_ID), add constraint FK_TEAM foreign key (TEAM_ID) references TEAMS (ID);
- insert into TEAMS(ID,NAME) values(1,'ABC Company');
- insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(1,'JM','Tom','yellow',null,1);
- insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(2,'JM','Mike','orange',null,1);
- insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(3,'CM','Jack',null,1.2,1);
- insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(4,'CM','Linda',null,2.0,1);
Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)的更多相关文章
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)
一. 1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- js 数组去重复键
Array.prototype.deleteEle = function() { var newArr = this; for (var i = newArr.length - 1; i >= ...
- Oracle 表的连接方式(2)-----HASH JOIN的基本机制1
我们对hash join的常见误解,一般包括两个: 第一个误解:是我们经常以为hash join需要对两个做join的表都做全表扫描 第二个误解:是经常以为hash join会选择比较小的表做buil ...
- openerp模块收藏 基于Lodop的报表打印模块(转载)
基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...
- Log Parser 2.2
Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 A ...
- JMS概述
[1.面向消息的中间件]顾名思义,面向消息的中间件就是通过使用消息(而不是命令)将企业内的组件连接起来的系统.例如库存系统可能会与工资和会计系统进行通信,如果使用面向消息的中间件将他们连接在一起,就可 ...
- Asp.Net生命周期系列五
如果您看了我的前四篇文章,应该知道目前Http请求已经流到了HttpModule这个程序员手中了,而且我们可以注册自己的HttpModule并且可以在里面注册一些事件来控制这个Http请求,但是到目前 ...
- DB天气app冲刺二阶段第八天
今天突然感觉应该做收尾工作了 因为马上就要考试了,时间一下子就不够用了.. 今天主要修复了一下bug,然后天气基本能够实时准确了,就是多功能按钮还是没有弄好 准备简化一下功能. 明天看看还有什么需要改 ...
- 图片轮播插件-carouFredSel
carouFredSel图片轮播插件基于Jquery,比较常规的轮播插件,支持滚轮及键盘左右按键,加入其它插件可实现更加复杂的特效. 主页地址:http://caroufredsel.dev7stud ...
- 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 ...
- 百度地图API使用
1.引用js脚本 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&am ...