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. 为 WordPress 标签添加 rel="nofollow" 属性

    WordPress 标签默认并无 rel="nofollow" 属性.rel="nofollow" 属性的作用是:告诉搜索引擎,无需追踪目标页,禁止蜘蛛爬行和传 ...

  2. IIS6下, web.config配置为targetFramework="4.0"时出404错误

    打开IIS管理器,在"Web 服务扩展" 中 将ASP.NET v4.0设置为允许就好了.这个选项默认是禁止的.

  3. 通过xsd生成xml类

    步骤二:使用VS2010 Tools中的命令提示窗口 如下图所示 执行结果:生成myschema.xsd对应的C#类文件. 命令剖析: /c  生成对应的类文件 /l:cs 类文件使用C#语言 /ou ...

  4. Spark菜鸟学习营Day1 从Java到RDD编程

    Spark菜鸟学习营Day1 从Java到RDD编程 菜鸟训练营主要的目标是帮助大家从零开始,初步掌握Spark程序的开发. Spark的编程模型是一步一步发展过来的,今天主要带大家走一下这段路,让我 ...

  5. MySQL 5.7.9多源复制报错修复

    版本:5.7.9 用5.7.9的版本搭建MySQL多源复制测试环境 开发说复制出现问题,上去看了一下: mysql> show slave status\G******************* ...

  6. c++各种数据类型表示范围

    符号属性     长度属性     基本型     所占位数     取值范围       输入符举例      输出符举例 --            --          char        ...

  7. Oppotunity land---China

    China is a land of opportunity.Following the development of China,every sector has made their contri ...

  8. [搜片神器]winform程序自己如何更新自己的方法代码

    DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 国外测试 ...

  9. java连接sqlserver2008报错 java.sql.SQLException: 对象名 '表名' 无效.

    注意:c3p0的数据库配置方式为: <named-config name="sqlsvr"> <property name="driverClass&q ...

  10. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...