首页 > 代码库 > hibernate中继承映射保存

hibernate中继承映射保存

1 简单继承映射,在子类上进行映射配置,可以将父类属性直接配置在子类映射文件中。

简单例子如下:teacher类继承自Person类。

技术分享
public class Person {
    private int id;
    private String pname;
    
    public Person() {
        // TODO Auto-generated constructor stub
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }

}
View Code

Teacher

技术分享
public class Teacher extends Person {
    private String tschoolName;

    public Teacher() {
        // TODO Auto-generated constructor stub
    }

    public String getTschoolName() {
        return tschoolName;
    }

    public void setTschoolName(String tschoolName) {
        this.tschoolName = tschoolName;
    }
}
View Code

Teacher.hbm.xml配置文件

<hibernate-mapping 
    package="com.baidu.entity3">
    
    <class name="Teacher" table="teacher">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        
        <property name="pname" column="name"></property>
        <property name="tschoolName" column="schoolName"></property>

    </class>
</hibernate-mapping>

简单测试

技术分享
public void fun1(){
        Teacher teacher = new Teacher();
        teacher.setPname("good");
        teacher.setTschoolName("南洋理工");
        
        Configuration configuration = new Configuration();
        configuration.configure();
        SessionFactory sessionFac = configuration.buildSessionFactory();
        Session session = sessionFac.openSession();
        Transaction bt = session.beginTransaction();
        
        session.save(teacher);
        bt.commit();
        
        session.close();
        sessionFac.close();
        
        
    }
View Code

 

2 所有类使用一张表,使用subclass属性。注意需要使用类鉴别器,这种设计方法不太符合数据库表的设计原则,会用很多空的字段产生。

继续上面的例子,新产生一个类,Engineer

技术分享
public class Engineer extends Person {
    
    private String ecompName;
    
    public Engineer() {
        // TODO Auto-generated constructor stub
    }

    public String getEcompName() {
        return ecompName;
    }

    public void setEcompName(String ecompName) {
        this.ecompName = ecompName;
    }
}
View Code

此时配置文件只在父类Person.hbm.xml映射文件中做

<hibernate-mapping 
    package="com.baidu.entity3">
    
    <class name="Person" table="person">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <discriminator column="type"></discriminator>
        <property name="pname" column="name"></property>
        
        <subclass name="Teacher" discriminator-value=http://www.mamicode.com/"teacher_">
            <property name="tschoolName" column="schoolName"></property>
        </subclass>
        
        <subclass name="Engineer" discriminator-value=http://www.mamicode.com/"engineer_">
            <property name="ecompName" column="eompanyName"></property>
        </subclass>

    </class>


</hibernate-mapping>

注意需要使用鉴别器,discriminator位置在id属性下面,property上面。每个子类使用subclass标签区分,需要指定discriminator-value属性,表明该类属于那个类别。

 

3 每个类使用一张表,属性关键字joined-subclass,也是在父类的配置文件上Person.hbm.xml配置

<hibernate-mapping 
    package="com.baidu.entity3">
    
    <class name="Person" table="person">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <discriminator column="type"></discriminator>
        <property name="pname" column="name"></property>
        
         <joined-subclass name="Engineer" table="engineer">
             <key column="id"></key>
             <property name="ecompName" column="companyName"></property>
         </joined-subclass>
         
       <joined-subclass name="Teacher" table="teacher">
             <key column="id"></key>
             <property name="tschoolName" column="schoolName"></property>
         </joined-subclass>
    </class>


</hibernate-mapping>

注意:因为每个子类都要生成一张表,因此在使用joined-subclass的时候需要指定table属性来指定使用那张表。

 

4 所有子类使用一张表,属性关键字union-subclass。注意这种方式主键的生成方式不可以使用自增长,可以使用uuid。配置文件还是在Person.hbm.xml配置

<hibernate-mapping 
    package="com.baidu.entity3">
    
    <class name="Person" table="person">
        <id name="id" column="id">
            <generator class="uuid"/>
        </id>
        <discriminator column="type"></discriminator>
        <property name="pname" column="name"></property>
        
        <union-subclass name="Engineer" table="engineer">
             <property name="ecompName" column="companyName"></property>
         </union-subclass>
         
         <union-subclass name="Teacher" table="teacher">
             <property name="tschoolName" column="schoolName"></property>
         </union-subclass>
    </class>


</hibernate-mapping>

 

hibernate中继承映射保存