Hibernate 中如何保存继承树

类别:Java 点击:0 评论:0 推荐:

Hibernate 中如何保存继承树

现有三个类。Person,Manager,Clerk,它们对应于数据库中的同一张表Tperson. Manager和Clerk是Person的子类。

类Person的代码如下:


package com.sorun.inherit.bean;
public class Person {
 private Long id;
 private String name;
 private String type;

 /**
  * @return Returns the id.
  */
 public Long getId() {
  return id;
 }
 /**
  * @param id The id to set.
  */
 public void setId(Long id) {
  this.id = id;
 }
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return Returns the type.
  */
 public String getType() {
  return type;
 }
 /**
  * @param type The type to set.
  */
 public void setType(String type) {
  this.type = type;
 }
}

类Manager和类Clerk只是简单的继承Person类。

代码 (Manager.java)

package com.sorun.inherit.bean;

public class Manager extends Person {
}

代码(Clerk.java)


package com.sorun.inherit.bean;

public class Clerk extends Person {
}


映射文件person.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
 package="com.sorun.inherit.bean">

 <class name="Person" table="Tperson" discriminator-value="P"
  >
  <!--  -->
  <id name="id">
   <generator class="identity"/>
  </id>

<!-- 试一下将force设为false效果会怎样?  -->
  <discriminator type="char" column="type" force="true"/>
  <property name="name" not-null="true"/>
  <subclass name="Manager"
   discriminator-value="M"/>
  <subclass name="Clerk"
   discriminator-value="C"/>  
 </class>
 
</hibernate-mapping>

测试用例:

 

package com.sorun.inherit.test;

import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import org.apache.log4j.Logger;

import com.sorun.inherit.bean.Manager;
import com.sorun.inherit.bean.Person;
import com.sorun.inherit.database.SessionFactory;
public class Main {
    public static void main(String[] args)throws Exception {
     Logger log=Logger.getLogger(Main.class);
        Person manager=new Manager();
        manager.setName("test");
        Session session=SessionFactory.getSession();
        Transaction trans=session.beginTransaction();
        session.save(manager);
        trans.commit();
        Query query=session.createQuery("from Person as person");
        List list=query.list();
        Iterator iterator=list.iterator();
        while (iterator.hasNext()){
         Person person=(Person)iterator.next();
         log.info(person);
        }
        session.close();
    }
}
输出结果:

Hibernate: insert into Tperson (name, type) values (?, 'M') select SCOPE_IDENTITY()
Hibernate: select person0_.id as id, person0_.type as type, person0_.name as name from Tperson person0_ where person0_.type in ('P', 'C', 'M')
##<2004/09/13  14:35:687><INFO><com.sorun.inherit.test.Main>(Main.java:42)<com.sorun.inherit.bean.Manager@15fc40c>
##<2004/09/13  14:35:687><INFO><com.sorun.inherit.test.Main>(Main.java:42)<com.sorun.inherit.bean.Manager@401369>
##<2004/09/13  14:35:687><INFO><com.sorun.inherit.test.Main>(Main.java:42)<com.sorun.inherit.bean.Manager@1a7ddcf>

Eclipse3.0 + Hibernate2.1.6 +SqlServer2000 +Windows2000

本文地址:http://com.8s8s.com/it/it15569.htm