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 ...
随机推荐
- php var_export与var_dump 输出的不同
var_export必须返回合法的php代码,var_export返回的代码,可以直接当作php代码赋值个一个变量. 而这个变量就会取得和被var_export一样的类型的值. 问题描述: 在跟踪 ...
- jqueryGrid 内置的onclickSubmit afterSubmit
$(document).ready(function() { $('#jpgCustomers').jqGrid({ //url from wich data should be requested ...
- [转]null和""以及==与equals的区别
String str1 = null; str引用为空 String str2 = ""; str引用为空串 直接点就是null没有分配内存空间,而""分配了内 ...
- ORACLE 11G 配置DG 报ORA-10458、ORA-01152、ORA-01110
操作系统: Oracle Linux Server release 5.7 数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.3 ...
- awk简明教程
我在这里的教程并不想面面俱到,全是示例,基本无废话. 我只想达到两个目的: 1)你可以在乘坐公交地铁上下班,或是在坐马桶拉大便时读完(保证是一泡大便的工夫). 2)我只想让这篇博文像一个火辣的脱衣舞女 ...
- [css]兼容性
div +input 输入框 , 在微信中 有问题 块级元素 行内元素
- 【go】sdk + idea-plugin 开发工具安装
http://golang.org/doc/install/source 第一步:windows 安装 git第二步$ git clone https://go.googlesource.com/go ...
- 从零开始学ios开发(八):Autorotation and Autosizing
不好意思,这一篇间隔的时间有点长,最近实在是事情太多,耽搁了,好了,长话短说,下面继续学习ios. 这次学习的内容是Autorotation和Autosizing,Autorotation就是屏幕内容 ...
- [转] c和python利用setsockopt获得端口重用
假如端口被socket使用过,并且利用socket.close()来关闭连接,但此时端口还没有释放,要经过一个TIME_WAIT的过程之后才能使用.为了实现端口的马上复用,可以选择setsockopt ...
- sky A800s手机恢复出厂设置操作
关机的情况下,手指一起按这3个按键(音量下键+搜索键+开机键)看到SKY标志后即可松手,就可以进入恢复界面恢复界面操作方法:音量上下键为上下移动,关机键为选择.选择 wipe data/factory ...