hibernate继承之---subclass 已更新(四)
先看由满江红翻译团队(RedSaga Translate Team)翻译的子类(subclass)说明,再来看我们的例子
多态持久化需要为父类的每个子类都进行定义。对于“每一棵类继承树对应一个表”的策略来说,就需要使用 <subclass>定义。
好了。。
开始看例子了。。
建表
父类
Dog
配置文件。。精华在这了.
测试类
多态持久化需要为父类的每个子类都进行定义。对于“每一棵类继承树对应一个表”的策略来说,就需要使用 <subclass>定义。
<subclass name="ClassName" (1) discriminator-value="discriminator_value" (2) proxy="ProxyInterface" (3) lazy="true|false" (4) dynamic-update="true|false" dynamic-insert="true|false" entity-name="EntityName" node="element-name"> <property .... /> ..... </subclass>
(1) | name: 子类的全限定名。 |
(2) | discriminator-value(辨别标志) (可选 - 默认为类名):一个用于区分每个独立的子类的值。 |
(3) | proxy(代理) (可选): 指定一个类或者接口,在延迟装载时作为代理使用。 |
(4) | lazy (可选, 默认是true): 设置为 lazy="false" 禁止使用延迟抓取 |
每个子类都应该定义它自己的持久化属性和子类。 <version> 和<id> 属性可以从根父类继承下来。在一棵继承树上的每个子类都必须定义一个唯一的discriminator-value。如果没有指定,就会使用Java类的全限定名。
可以在单独的映射文件中,直接在hibernate-mapping下定义subclass,union-subclass和joined-subclass映射。这样你只要增加一个新的映射文件就可以继承一棵类继承树。你必须在子类的映射中指定extends 属性来指定已映射的超类。注意:以前,这个特性使得映射文件的顺序变得很重要。从Hibernate3开始,当使用extends关键字的时候,映射文件的次序便不重要了。而在单一映射文件中,依旧需要保持将超类定义在子类之前这样的次序。
<hibernate-mapping> <subclass name="DomesticCat" extends="Cat" discriminator-value="D"> <property name="name" type="string"/> </subclass> </hibernate-mapping>
好了。。
开始看例子了。。
建表
create table animal
(aid varchar( 32 ) not null primary key,
atype varchar( 16 ),
dname varchar( 16 ),
cname varchar( 16 )
)
(aid varchar( 32 ) not null primary key,
atype varchar( 16 ),
dname varchar( 16 ),
cname varchar( 16 )
)
父类
package
com.test;
public class Animal
{
private String aid;
//geter seter
Cat
public class Animal
{
private String aid;
//geter seter
package
com.test;
public class Cat extends Animal
{
private String cname;
//geter seter
public class Cat extends Animal
{
private String cname;
//geter seter
Dog
package
com.test;
public class Dog extends Animal
{
private String dname;
//geter seter
public class Dog extends Animal
{
private String dname;
//geter seter
配置文件。。精华在这了.
<?
xml version="1.0"
?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name ="com.test.Animal" table ="animal" >
< id name ="aid" type ="string" unsaved-value ="null" >
< column name ="aid" sql-type ="char(32)" not-null ="true" />
< generator class ="uuid.hex" />
</ id >
< discriminator column ="atype" type ="string" />
< subclass name ="com.test.Dog" discriminator-value ="dog" >
< property name ="dname" >
< column name ="dname" sql-type ="varchar(16)" />
</ property >
</ subclass >
< subclass name ="com.test.Cat" discriminator-value ="cat" >
< property name ="cname" >
< column name ="cname" sql-type ="varchar(16)" />
</ property >
</ subclass >
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name ="com.test.Animal" table ="animal" >
< id name ="aid" type ="string" unsaved-value ="null" >
< column name ="aid" sql-type ="char(32)" not-null ="true" />
< generator class ="uuid.hex" />
</ id >
< discriminator column ="atype" type ="string" />
< subclass name ="com.test.Dog" discriminator-value ="dog" >
< property name ="dname" >
< column name ="dname" sql-type ="varchar(16)" />
</ property >
</ subclass >
< subclass name ="com.test.Cat" discriminator-value ="cat" >
< property name ="cname" >
< column name ="cname" sql-type ="varchar(16)" />
</ property >
</ subclass >
</ class >
</ hibernate-mapping >
测试类
package
com.test;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate. * ;
import java.sql. * ;
import java.util.List;
import java.util.Iterator;
public class TestInherit
{
SessionFactory sf;
Session session;
public TestInherit()
{
try
{
Configuration cfg = new Configuration();
sf = cfg.addClass(Animal.class).buildSessionFactory();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
//插入
public void doCreate()
{
try
{
session = sf.openSession();
Dog dog = new Dog();
dog.setDname("small dog");
Cat cat = new Cat();
cat.setCname("small cat");
session.save(dog);
session.save(cat);
session.flush();
session.connection().commit();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
catch(SQLException ex1)
{
ex1.printStackTrace();
}
finally
{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
//查询
public void doQuery()
{
try{
session = sf.openSession();
Query q = session.createQuery("select d from Dog as d");
List l = q.list();
Dog dog = null;
Cat cat = null;
for(int i=0;i<l.size();i++)
{
dog = (Dog)l.get(i);
System.out.println(dog.getDname());
}
}
catch(HibernateException ex){
ex.printStackTrace();
}
finally{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
public static void main(String[] args)
{
TestInherit t = new TestInherit();
//t.doCreate();
t.doQuery();
}
}
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate. * ;
import java.sql. * ;
import java.util.List;
import java.util.Iterator;
public class TestInherit
{
SessionFactory sf;
Session session;
public TestInherit()
{
try
{
Configuration cfg = new Configuration();
sf = cfg.addClass(Animal.class).buildSessionFactory();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
//插入
public void doCreate()
{
try
{
session = sf.openSession();
Dog dog = new Dog();
dog.setDname("small dog");
Cat cat = new Cat();
cat.setCname("small cat");
session.save(dog);
session.save(cat);
session.flush();
session.connection().commit();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
catch(SQLException ex1)
{
ex1.printStackTrace();
}
finally
{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
//查询
public void doQuery()
{
try{
session = sf.openSession();
Query q = session.createQuery("select d from Dog as d");
List l = q.list();
Dog dog = null;
Cat cat = null;
for(int i=0;i<l.size();i++)
{
dog = (Dog)l.get(i);
System.out.println(dog.getDname());
}
}
catch(HibernateException ex){
ex.printStackTrace();
}
finally{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
public static void main(String[] args)
{
TestInherit t = new TestInherit();
//t.doCreate();
t.doQuery();
}
}