站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse快速上手Hibernate--4. 继承映射(1)

Eclipse快速上手Hibernate--4. 继承映射(1)

摘要: Eclipse快速上手Hibernate--4. 继承映射(1)
前面的《Eclipse快速上手Hibernate--1. 入门实例 》等三篇文章已经谈了Hibernate的入门以及利用工具创建的方法。这篇文章主要说说在Hibernate中的继承映射。相关配置请参考前三篇文章。 如果程序中的对象含有继承的关系,在Hibernate中有以下三种策略将这种关系映射到数据表上:· 每个类层次结构一个表(table per class hierarchy)· 每个子类一个表(table per subclass)
· 每个具体类一个表(table per concrete class)(有一些限制) 每个类层次结构一个表的方式是将所有继承同一父类别的对象储存在同一个表格中,为了做到这一点,需要在表格中使用识别字段来表示某一列(row)是属于某个子类别或父类别,在这个主题中我们将先说明这个方法。
1. 创建项目 · 新建一个Java项目:InheritanceMapping,注意选中“创建单独的源文件夹和输出文件夹”,同时添加“用户库”:hibernate。
2. 编写类文件 · 新建一个类,包名:javamxj.inheritance.one,类名:Animal。然后在生成的代码中添加变量,再利用“生成 Getter 和 Setter”,具体方式同《Eclipse快速上手Hibernate--1. 入门实例 》文章中的编辑User.java的方式一样。

Animal.java

/*

 * Hibernate - 继承映射(每个类层次一个表)

 * 创建日期 2005-4-9

 * @author javamxj(分享java快乐)

 * @link  Blog: htpp://javamxj.mblogger.cn 

 *              htpp://blog.csdn.net/javamxj/

 */

package javamxj.inheritance.one;

/**

 * @hibernate.class

 *   table="Animal"

 *   discriminator-value="Animal"

 * @hibernate.discriminator

 *   column="ANIMAL_TYPE"

 *   type="string"

 *   length = "10"

 */

public abstract class Animal {

private Long id;

private String name;

/**

 * @hibernate.id

 *   column="ID"

 *   generator-class="hilo"

 *   unsaved-value="null"

 */

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

/**

 * @hibernate.property

 *   length = "24"

 */

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public abstract void makeSound();

}

· 这个类是父类,值得注意是在类层次标记中添加了一个discriminator标记,并用它定义了一个字段“ANIMAL_TYPE”,这个字段就是用来识别某一列(row)是属于某个子类别或父类别的。 · 子类Cat.java
<table bordercolor="#000000" cellspacing="0" cellpadding="4" width="70%" border="0"><tr><td style="FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-BOTTOM: rgb(51,51,204) thin inset; FONT-STYLE: italic; FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#cccccc" height="16">
Cat.java</td></tr><tr><td style="FONT-SIZE: 12px; BORDER-TOP-STYLE: none; BORDER-TOP-COLOR: rgb(236,233,216); FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#e2e2e2">
package javamxj.inheritance.one;

/**

 * @hibernate.subclass

 *   discriminator-value="Cat"

 */

public class Cat extends Animal {

private String FurColor;

public void makeSound() {

System.out.println("喵喵");

}

/**

 * @hibernate.property

 *   length = "24"

 */

public String getFurColor() {

return FurColor;

}

public void setFurColor(String furColor) {

FurColor = furColor;

}

}

</td></tr></table>
· 子类Dog.java
<table bordercolor="#000000" cellspacing="0" cellpadding="4" width="70%" border="0"><tr><td style="FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-BOTTOM: rgb(51,51,204) thin inset; FONT-STYLE: italic; FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#cccccc" height="16">
Dog.java</td></tr><tr><td style="FONT-SIZE: 12px; BORDER-TOP-STYLE: none; BORDER-TOP-COLOR: rgb(236,233,216); FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#e2e2e2">
package javamxj.inheritance.one;

/**

 * @hibernate.subclass

 *   discriminator-value="Dog"

 */

public class Dog extends Animal {

private String category;

public void makeSound() {

System.out.println("汪汪");

}

/**

 * @hibernate.property

 *   length = "24"

 */

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

}

</td></tr></table>
· 这两个子类都很简单,注意添加的hibernate.subclass的标记,指定其识别字段。
3. 构建文件Build.xml

· 在项目根目录下建立一个build.xml,要注意的是环境变量和数据库的设置要符合自己的实际配置,这里库文件目录的设置是"D:/java/Hibernate/lib",参考文章《Eclipse快速上手Hibernate--1. 入门实例》中的设置。· 在MySQL中需要先建立一个HibernateTest数据库,为了解决中文问题,使用了GBK编码,注意“&amp;amp;”,这是由于XDoclet生成Hibernate配置文件时,会丢失一个“amp;”字符串(一个小Bug)。· 这里我用build.xml中的“hibernatedoclet”任务直接生成“hibernate.cfg.xml”配置文件。

build.xml

<?xml version="1.0" encoding="GBK"?>
































































































· 好了,只要这四个文件就够了,其它的会自动生成的。整个项目的结构如下:
4. 运行任务
· 双击“generate-hbm”任务,会发现在包中多了一个Animal.hbm.xml文件,在src目录下会多了一个hibernate.cfg.xml文件,如果没有,按F5键刷新一下(这里建议打开Eclipse的“首选项”对话框,在“工作台”中勾选“自动刷新工作空间”和“在构建之前自动保存”这两项,这样以后不用每次都刷新了)。

Animal.hbm.xml

<?xml version="1.0" encoding="GBK"?>

 hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">



    

        

            

             

                  To add non XDoclet generator parameters, create a file named

                  hibernate-generator-params-Animal.xml

                  containing the additional parameters and place it in your merge dir.

              

            

        

        

        

       

            To add non XDoclet property mappings, create a file named

                hibernate-properties-Animal.xml

            containing the additional properties and place it in your merge dir.

       

        

        

   

            To add non XDoclet property mappings, create a file named

                hibernate-properties-Cat.xml

containing the additional properties and place it in your merge dir.

   

        

        

        

   

            To add non XDoclet property mappings, create a file named

                hibernate-properties-Dog.xml

containing the additional properties and place it in your merge dir.

   

        

    



<table bordercolor="#000000" cellspacing="0" cellpadding="4" width="70%" border="0"><tr><td style="FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-BOTTOM: rgb(51,51,204) thin inset; FONT-STYLE: italic; FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#cccccc" height="16">
hibernate.cfg.xml</td></tr><tr><td style="FONT-SIZE: 12px; BORDER-TOP-STYLE: none; BORDER-TOP-COLOR: rgb(236,233,216); FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#e2e2e2">
<?xml version="1.0" encoding="GBK"?>

 hibernate-configuration

  PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN"

  "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">





net.sf.hibernate.dialect.MySQLDialect

true

false

root

javamxj

com.mysql.jdbc.Driver

           jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;characterEncoding=GBK







</td></tr></table>
· 双击“schemaexport”任务,在项目根目录下,会产生一个“schema-export.sql”文件。<table bordercolor="#000000" cellspacing="0" cellpadding="4" width="70%" border="0"><tr><td style="FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-BOTTOM: rgb(51,51,204) thin inset; FONT-STYLE: italic; FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#cccccc" height="16">
schema-export.sql</td></tr><tr><td style="FONT-SIZE: 12px; BORDER-TOP-STYLE: none; BORDER-TOP-COLOR: rgb(236,233,216); FONT-FAMILY: Courier New" valign="top" width="100%" bgcolor="#e2e2e2">drop table if exists Animal
drop table if exists hibernate_unique_key
create table Animal (
ID bigint not null,
ANIMAL_TYPE varchar(10) not null,
name varchar(24),
furColor varchar(24),
category varchar(24),
primary key (ID)
)
create table hibernate_unique_key (
next_hi integer
)
insert into hibernate_unique_key values ( 0 )</td></tr></table>
· 切换到数据库中,会发现已经自动产生了数据表animal(表hibernate_unique_key是由于采用hilo主键策略生成的)。
5. 测试程序 · 好了,在包javamxj.inheritance.one下新建一个Demo.java类,很简单,前半部分是添加数据,后半部分是简单的测试。

Demo.java

package javamxj.inheritance.one;

import java.util.Iterator;

import java.util.List;

import net.sf.hibernate.HibernateException;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.Transaction;

import net.sf.hibernate.cfg.Configuration;

public class Demo {

public static void main(String[] args) {

try {

new Demo();

} catch (HibernateException he) {

he.printStackTrace();

}

}

public Demo() throws HibernateException {

SessionFactory sf = new Configuration().configure()

.buildSessionFactory();

Session sess = sf.openSession();

Transaction tx = null;

try {

tx = sess.beginTransaction();

Cat cat = new Cat();

cat.setName("小白");

cat.setFurColor("白色");

sess.save(cat);

Dog dog = new Dog();

dog.setName("小黑");

dog.setCategory("京巴狗");

sess.save(dog);

tx.commit();

} catch (HibernateException e) {

if (tx != null)

tx.rollback();

throw e;

} finally {

sess.close();

}

sess = sf.openSession();

tx = null;

try {

tx = sess.beginTransaction();

List animals = sess.find("from " + Animal.class.getName());

for (Iterator it = animals.iterator(); it.hasNext();) {

Animal animal = (Animal) it.next();

System.out.println("动物 '" + animal.getName()

+ "' 所在类是: " + animal.getClass().getName());

System.out.print("发出叫声: ");

animal.makeSound();

}

tx.commit();

} catch (HibernateException e) {

if (tx != null)

tx.rollback();

throw e;

} finally {

sess.close();

}

}

}

· 运行这个类,控制台输出如下: · 同时,数据表中生成如下数据:注意其中为“NULL”的部分。 · 最后的项目结构如下: 小结: ● 优点:
· 实现简单。
· 支持多态——对象角色发生变化,或存在多重角色时。
· 报表操作实现简单:表中包含了所有信息。
● 缺点:
· 增加类层次中的耦合。类层次中任何类的属性的增加都会导致表的变更;某个子类属性的修改会影响到整个
层次结构,而不仅仅是该子类。
· 浪费了一些数据库空间。浪费空间的多少取决于继承层次的深度。层次越深,不同的属性越多,属性的全集就越大,也就越浪费空间。
· 可能需要指明具体的角色。 参考:· HIBERNATE - 符合Java习惯的关系数据库持久化(第8章)· Hibernate 简化继承映射· Mapping Objects to Relational Databases: O/R Mapping In Detail· Mapping objects to relational databases 下篇文章会谈谈每个子类一个表的策略。

↑返回目录
前一篇: Eclipse快速上手Hibernate--4. 继承映射(2)
后一篇: Eclipse Form程序设计指南(3)