Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
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.CMonkey" table="CMONKEYS">
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" column="NAME" /> <property name="length" column="LENGTH" type="double" /> <many-to-one
name="team"
column="TEAM_ID"
class="mypack.Team"
/> </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.JMonkey" table="JMONKEYS">
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" column="NAME" /> <property name="color" column="COLOR" type="string" /> <many-to-one
name="team"
column="TEAM_ID"
class="mypack.Team"
/> </class> </hibernate-mapping>
4.
<?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" /> </class>
</hibernate-mapping>
5.
 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;
     }
 }
6.
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;
}
}
7.
 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;
     }
 }
8.
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;
}
}
9.
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 findAllMonkeys(){
Session session = sessionFactory.openSession();
Transaction tx = null; try {
List results=new ArrayList();
tx = session.beginTransaction();
List jMonkeys=session.createQuery("from JMonkey").list();
results.addAll(jMonkeys); List cMonkeys=session.createQuery("from CMonkey").list();
results.addAll(cMonkeys); 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)); List jMonkeys=session
.createQuery("from JMonkey j where j.team.id="+id)
.list();
team.getMonkeys().addAll(jMonkeys); List cMonkeys=session
.createQuery("from CMonkey c where c.team.id="+id)
.list();
team.getMonkeys().addAll(cMonkeys); tx.commit();
return team;
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void test() {
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();
}
}

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 JMONKEYS (
ID bigint not null,
NAME varchar(15),
COLOR varchar(15),
TEAM_ID bigint,
primary key (ID)
);
create table CMONKEYS (
ID bigint not null,
NAME varchar(15),
LENGTH double precision,
TEAM_ID bigint,
primary key (ID)
);
alter table JMONKEYS add index IDX1_TEAM(TEAM_ID), add constraint FK1_TEAM foreign key (TEAM_ID) references TEAMS (ID);
alter table CMONKEYS add index IDX2_TEAM(TEAM_ID), add constraint FK2_TEAM foreign key (TEAM_ID) references TEAMS (ID); insert into TEAMS(ID,NAME) values(1,'NBA'); insert into JMONKEYS(ID,NAME,COLOR,TEAM_ID) values(1,'Tom','yellow',1); insert into JMONKEYS(ID,NAME,COLOR,TEAM_ID) values(2,'Mike','orange',1); insert into CMONKEYS(ID,NAME,LENGTH,TEAM_ID)
values(1,'Jack',1.2,1); insert into CMONKEYS(ID,NAME,LENGTH,TEAM_ID)
values(2,'Linda',2.0,1);
11.
<?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/JMonkey.hbm.xml" />
<mapping resource="mypack/CMonkey.hbm.xml" />
</session-factory>
</hibernate-configuration>
Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表的更多相关文章
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
		
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
 - Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
		
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
 - Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
		
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章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
		
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
 - 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章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
		
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
 
随机推荐
- 百度云盘demo
 - 拥抱ARM妹纸第二季 之 第三次 给我变个月亮,让约会更浪漫!
			
嗯嗯,效果不错.趁着这个热乎劲,接到俺的LED测试板上试试.呃~~~ 竟然和小LED的效果不一样啊,不一样.不但闪烁而且完全没有调光效果.郁闷内,查查原因呗.看看那里出问题.迅速在PT4115手册里翻 ...
 - [转]DataGridView绑定泛型List的种种
			
1.DataGridView数据绑定对比(DataTable与泛型List):当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...
 - Sales_item
			
#ifndef SALESITEM_H #define SALESITEM_H // Definition of Sales_item class and related functions goes ...
 - 查看MYSQL数据库中所有用户及拥有权限
			
查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...
 - MVC下用C#实现Excel导出
			
Aspx页面脚本: function exportxls() { window.open("/Common/HomeExport?startdate=" + $("#hi ...
 - description 数组的中文打印
			
打印一个对象:NSLog(@"%@", stu); 默认情况下打印的时对象的名字和内存地址:这时需要重写description方法 // 重写description方法 - (NS ...
 - iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述
			
本文档尝试用Video Toolbox进行H.265(HEVC)硬件编码,视频源为iPhone后置摄像头.去年做完硬解H.264,没做编码,技能上感觉有些缺失.正好刚才发现CMFormatDescri ...
 - SecureCRT配色方案
			
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.作为一款经常使用的终端软件,一个好的配色方案可以大大的提高学 ...
 - org.apache.commons.dbutils.QueryRunner  执行sqlserver的存储过程
			
执行不带输出参数的存储过程与 执行普通update sql没有什么区别,直接调用即可: 示例代码: public Boolean startResidentialInfoStatistics(Str ...