类继承树映射成一张表

表结构:

create table j_people2
(
   Id number,
   Name varchar(20),
   Age number,
   type number,
   School varchar(30),
   Company varchar(30)
   
);

实体类People:

public class PeopleT  implements java.io.Serializable {


    // Fields    

     private Long id;
     private String name;
     private int age;
    /* private int type;
     private String school;
     private String company;*/
     //实体类和数据表之间并不是一一对应关系,实体类存在实体类间的关系,数据表存在数据表的构造组织方式,hbm.xml映射文件是关联迁就双方的工具
     //本来就不是完全映射的关系


    // Constructors

    /** default constructor */
    public PeopleT() {
    }


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public int getAge() {
		return age;
	}


	public void setAge(int age) {
		this.age = age;
	}
    
    
	

}


子类Student:

public class Student extends PeopleT {

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}

子类Staff:

public class Staff extends PeopleT{
 
	private String company;

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}
	
	
}

关键的映射文件:

<hibernate-mapping>
    <class name="com.sjr.bean.PeopleT" table="J_PEOPLE2" schema="SXBBKF" discriminator-value="not null">   --这里是调整鉴别器的
        <id name="id" type="long">
            <column name="ID" precision="22" scale="0" />
            <generator class="native"></generator>
        </id>
        
         <discriminator column="type" type="int"/>  --默认为string,需要转化才可以int等其他类型
         
        <property name="name" type="string">
            <column name="NAME" length="20" />
        </property>
        <property name="age" type="int">
            <column name="AGE" precision="22" scale="0" />
        </property>
        
        <subclass name="com.sjr.bean.Student" discriminator-value='1'>
        <property name="school"></property>
        </subclass>
        
        <subclass name="com.sjr.bean.Staff" discriminator-value='2'>
        <property name="company"></property>
        </subclass>
        
        <!--
        <property name="school" type="string">
            <column name="SCHOOL" length="30" />
        </property>
        <property name="company" type="string">
            <column name="COMPANY" length="30" />
        </property>
        -->
        
        </class>
</hibernate-mapping>

结果:

Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into SXBBKF.J_PEOPLE2 (NAME, AGE, school, type, ID) values (?, ?, ?, 1, ?)
Hibernate: insert into SXBBKF.J_PEOPLE2 (NAME, AGE, company, type, ID) values (?, ?, ?, 2, ?)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值