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继承关系树中的每个具体类对应一个表的更多相关文章

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

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

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

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

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

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

  4. Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

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

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

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

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

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

  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章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)

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

随机推荐

  1. 简单的下拉刷新以及优化--SwipeRefreshLayout

    代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...

  2. C#生成随机字符串

    //<summary> ///得到随机字符. ///</summary> ///<param name="intLength">Length o ...

  3. WPF自定义控件(二)——TextBox

    和之前一样,先来看看效果: 这个TextBox可设置水印,可设置必填和正则表达式验证. 验证?没错,就是验证! 就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进 ...

  4. tomcat中server.xml文件解析

    下面我们将讲述这个文件中的基本配置信息,更具体的配置信息见tomcat的文档 元素名 属性 解释 server port 指定一个端口,这个端口负责监听关闭tomcat的请求 shutdown 指定向 ...

  5. openwrt无线中继教程

    1.设置自己路由lan口的IP地址,网段不能跟上级路由的一样. 2.在无线标签下点击"扫描网络". 3.在新出现的界面中,会列出你附近的无线网络.点击你需要中继的网络右边的&quo ...

  6. "=="和equals方法究竟有什么区别

    (单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同, ...

  7. 升级Mac osx 10.10 后安装php 的mcrypt extension

    昨天折腾到凌晨四点多,今天又到这个点,终于成功了,心情比较复杂,先记录一下,改日再整理,此刻头昏脑胀,只想蒙头大睡. 发的问题,还没人回复,自己找到答案了,英语比较蹩脚,凑合看.. http://st ...

  8. matlab实现贝塞尔曲线绘图pdf查看

    贝塞尔曲线绘图方法: %Program 3.7 Freehand Draw Program Using Bezier Splines %Click in Matlab figure window to ...

  9. Pomodairo,番茄工作法-解刨篇

    处于“信息大爆炸”的 e 时代的我们每天必定要处理很多的事情,不管是工作.学习.生活......面对这么多的纷杂的事物我们将如何应对?如何做到有条不紊的进行?高效.轻松.愉快的完成它呢?这时一款精致的 ...

  10. css/js online online code editor/formator/debuger

    http://cssdeck.com/labs http://jsfiddle.net/ http://fiddle.jshell.net/  support console http://plnkr ...