Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7593443 建立时间:2006年5月29日 |

| |
[Hibernate]hibernate继承---联合子类 已更新(六) 软件技术
lhwork 发表于 2007/1/7 14:08:23 |
这篇和上一篇http://www.blogjava.net/wujun/archive/2006/04/11/40527.html不同的地方在于配置文件上。上一篇采用的是joined-subclass 先看先看由满江红翻译团队(RedSaga Translate Team)翻译的联合子类说明..
<union-subclass name="ClassName" (1) table="tablename" (2) proxy="ProxyInterface" (3) lazy="true|false" (4) dynamic-update="true|false" dynamic-insert="true|false" schema="schema" catalog="catalog" extends="SuperclassName" abstract="true|false" persister="ClassName" subselect="SQL expression" entity-name="EntityName" node="element-name"> <property .... /> .....</union-subclass>
(1)
name: 子类的全限定名。
(2)
table: 子类的表名
(3)
proxy (可选): 指定一个类或者接口,在延迟装载时作为代理使用。
(4)
lazy (可选, 默认是 true): 设置为 lazy="false" 禁止使用延迟装载。
这种映射策略不需要指定辨别标志(discriminator)字段。
更多关于继承映射的信息,参考第 10 章 继承映射(Inheritance Mappings)。
好了。。看 一个例子看这个配置文件
500)this.width=500'>
<?
xml version="1.0"
?>
500)this.width=500'>
<!
DOCTYPE hibernate-mapping500)this.width=500'> PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"500)this.width=500'> "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
500)this.width=500'>
500)this.width=500'>
<
hibernate-mapping
>
500)this.width=500'>
500)this.width=500'>
<
class
name
="com.test.Dog"
table
="dog"
>
500)this.width=500'>500)this.width=500'>
<
id
name
="aid"
type
="string"
unsaved-value
="null"
>
500)this.width=500'>
<
column
name
="aid"
sql-type
="char(32)"
not-null
="true"
/>
500)this.width=500'>
<
generator
class
="uuid.hex"
/>
500)this.width=500'>
</
id
>
500)this.width=500'>
500)this.width=500'>
<
property
name
="dname"
>
500)this.width=500'>
<
column
name
="dname"
sql-type
="varchar(16)"
/>
500)this.width=500'>
</
property
>
500)this.width=500'>
500)this.width=500'>
</
class
>
500)this.width=500'>
500)this.width=500'>
<
class
name
="com.test.Cat"
table
="cat"
>
500)this.width=500'>500)this.width=500'>
<
id
name
="aid"
type
="string"
unsaved-value
="null"
>
500)this.width=500'>
<
column
name
="aid"
sql-type
="char(32)"
not-null
="true"
/>
500)this.width=500'>
<
generator
class
="uuid.hex"
/>
500)this.width=500'>
</
id
>
500)this.width=500'>
500)this.width=500'>
<
property
name
="cname"
>
500)this.width=500'>
<
column
name
="cname"
sql-type
="varchar(16)"
/>
500)this.width=500'>
</
property
>
500)this.width=500'>
500)this.width=500'>
</
class
>
500)this.width=500'>
500)this.width=500'>
</
hibernate-mapping
>
表结构:
500)this.width=500'>
create
table
dog500)this.width=500'>(aid
varchar
(
32
)
not
null
primary
key
,500)this.width=500'> dname
varchar
(
16
)500)this.width=500'>)500)this.width=500'>500)this.width=500'>
create
table
cat500)this.width=500'>(aid
varchar
(
32
)
not
null
primary
key
,500)this.width=500'> cname
varchar
(
16
)500)this.width=500'>)
写VOAmimal.java
500)this.width=500'>
package
com.test;500)this.width=500'>500)this.width=500'>
public
class
Animal500)this.width=500'>500)this.width=500'>
500)this.width=500'>
{500)this.width=500'>
private
String aid;500)this.width=500'>
//
get set
Dog.java
500)this.width=500'>
package
com.test;500)this.width=500'>500)this.width=500'>
public
class
Dog
extends
Animal500)this.width=500'>500)this.width=500'>
500)this.width=500'>
{500)this.width=500'>
private
String dname;500)this.width=500'>
//
get set
Cat.java500)this.width=500'>package com.test;500)this.width=500'>500)this.width=500'>public class Cat extends Animal500)this.width=500'>500)this.width=500'>500)this.width=500'>{500)this.width=500'> private String cname;500)this.width=500'> //get set测试。。。500)this.width=500'>//插入500)this.width=500'> public void doCreate()500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'> try500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'> session = sf.openSession();500)this.width=500'>500)this.width=500'> Dog dog = new Dog();500)this.width=500'> dog.setDname("small dog");500)this.width=500'> Cat cat = new Cat();500)this.width=500'> cat.setCname("small cat");500)this.width=500'> session.save(dog);500)this.width=500'> session.save(cat);500)this.width=500'> session.flush();500)this.width=500'> session.connection().commit();500)this.width=500'>500)this.width=500'> }500)this.width=500'> catch(HibernateException ex)500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'> ex.printStackTrace();500)this.width=500'> }500)this.width=500'> catch(SQLException ex1)500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'> ex1.printStackTrace();500)this.width=500'> }500)this.width=500'> finally500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'>500)this.width=500'> try500)this.width=500'>{500)this.width=500'> session.close();500)this.width=500'> }500)this.width=500'>500)this.width=500'> catch(HibernateException ex2)500)this.width=500'>{500)this.width=500'> }500)this.width=500'> }500)this.width=500'>500)this.width=500'> }500)this.width=500'> //查询500)this.width=500'> public void doQuery()500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'>500)this.width=500'> try500)this.width=500'>{500)this.width=500'> session = sf.openSession();500)this.width=500'> Query q = session.createQuery("select d from Dog as d");500)this.width=500'> List l = q.list();500)this.width=500'> Dog dog = null;500)this.width=500'> Cat cat = null;500)this.width=500'> for(int i=0;i<l.size();i++)500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'> dog = (Dog)l.get(i);500)this.width=500'> System.out.println(dog.getDname());500)this.width=500'> }500)this.width=500'> }500)this.width=500'>500)this.width=500'> catch(HibernateException ex)500)this.width=500'>{500)this.width=500'> ex.printStackTrace();500)this.width=500'> }500)this.width=500'>500)this.width=500'> finally500)this.width=500'>{500)this.width=500'>500)this.width=500'> try500)this.width=500'>{500)this.width=500'> session.close();500)this.width=500'> }500)this.width=500'>500)this.width=500'> catch(HibernateException ex2)500)this.width=500'>{500)this.width=500'> }500)this.width=500'> }500)this.width=500'> }
|
|
|