Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-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>
<property name="name" type="string" column="NAME" /> <many-to-one
name="team"
column="TEAM_ID"
class="mypack.Team"
/> <joined-subclass name="mypack.JMonkey" table="JMONKEYS" >
<key column="MONKEY_ID" />
<property name="color" column="COLOR" type="string" />
</joined-subclass> <joined-subclass name="mypack.CMonkey" table="CMONKEYS" >
<key column="MONKEY_ID" />
<property name="length" column="LENGTH" type="double" />
</joined-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;
import java.io.Serializable;
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),
TEAM_ID bigint,
primary key (ID)
); create table JMONKEYS (
MONKEY_ID bigint not null,
COLOR varchar(15),
primary key (MONKEY_ID)
); create table CMONKEYS (
MONKEY_ID bigint not null,
LENGTH double precision,
primary key (MONKEY_ID)
); alter table MONKEYS add index IDX_TEAM(TEAM_ID), add constraint FK_TEAM foreign key (TEAM_ID) references TEAMS (ID); alter table JMONKEYS add index IDX_MONKEY1(MONKEY_ID), add constraint FK_MONKEY1 foreign key (MONKEY_ID) references MONKEYS (ID); alter table CMONKEYS add index IDX_MONKEY2(MONKEY_ID), add constraint FK_MONKEY2 foreign key (MONKEY_ID) references MONKEYS (ID); insert into TEAMS(ID,NAME) values(1,'NBA'); insert into MONKEYS(ID,NAME,TEAM_ID) values(1,'Tom',1);
insert into JMONKEYS(MONKEY_ID,COLOR) values(1,'yellow'); insert into MONKEYS(ID,NAME,TEAM_ID) values(2,'Mike',1);
insert into JMONKEYS(MONKEY_ID,COLOR) values(2,'orange'); insert into MONKEYS(ID,NAME,TEAM_ID) values(3,'Jack',1);
insert into CMONKEYS(MONKEY_ID,LENGTH) values(3,1.2); insert into MONKEYS(ID,NAME,TEAM_ID) values(4,'Linda',1);
insert into CMONKEYS(MONKEY_ID,LENGTH) values(4,2.0);
11.


Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)的更多相关文章
- Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、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章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- 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章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- How to disable Passwords must meet complexity requirements[windows 7]
The Password complexity is a Local Policy setting named "Passwords must meet complexity require ...
- IOC学习
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...
- C# 刷票程序
上个月有人让我帮忙投票,我想要不写个程序给他多刷点得了,虽然这事情有悖原则,就当娱乐了.. 先上图 1.分析 既然是网页投票,那肯定可以伪造HTTP请求来实现刷票.需要分析的就是该网站到底采用了哪些防 ...
- 【转】Eazfuscator.NET 3.3中混淆化需要注意的一些问题
对于DLL,Eazfuscator.NET默认不会混淆化任何公共成员,因为类库的公共成员很有可能被外界调用,而对于EXE的程序集,所有类型都可能被混淆化.注意上面这句话有一个“可能”,因为Eazfus ...
- SQL Server Management Studio Keyboard shortcuts
一些平时在SQL Server Management Studio 使用到的快捷键 F5 (Ctrl+x)执行选中部分的语句,没有选中则全文执行 Ctrl+L 现实执行计划(估计) Ctrl+M 在运 ...
- linux下的汇编环境搭建(nasm)
第一步:先判断系统是否已经安装了nasm--------------->打开终端,执行whereis nasm :如果显示nasm: /usr/bin/nasm ,则已经安装:如果只显示nasm ...
- 从Keil 4升级到Keil 5的工程,想返回来用Keil 4打开
情景描述: 笔者电脑程序Keil 4升级到Keil 5,相应地,原来项目上的工程也在第一次用Keil 5打开的时候进行了升级.之后,由于客户需要开发资料,其版本为Keil 4,我尝试着用Keil 4打 ...
- javac。java版本切换
如果安装有多个Java版本时(有时候有些软件自行安装),怎样方便的进行切换呢.除了常见的设置环境变量外,今天学到了一种新的切换方法: update-alternatives --config java ...
- glog使用
How To Use Google Logging Library Glog 的基本使用方法在google code上有介绍:How To Use Google Logging Library ;最好 ...
- .NET中class和struct的区别
1.引言 提起class和struct,我们首先的感觉是语法几乎相同,待遇却天壤之别.历史将接力棒由面向过程编程传到面向对象编程,class和struct也背负着各自的命运前行.在我认为,struct ...