码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate映射关系配置(五)

时间:2017-09-22 22:37:19      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:pid   ble   XML   对象   nbsp   native   span   student   subclass   

多表继承:

Bean:

public class Person implements Serializable{

    private int id ;
    
    private String name ;
    
    private int age ;
}
public class Student extends Person implements Serializable {
    
    private String dream ;
    
    private Date birthday ;
}
public class Teacher extends Person implements Serializable{

    private String job ;
    
    private boolean merry ;
}

xml(在同一个表中,加一个字段来区分类型):

Person.hbm.xml:
<class name = "Person" discriminator-value="p">
        <id name = "id">
            <generator class="native" />
        </id>
        <!-- discriminator 是新建一个列,此列用来区分对象具体的类型 
             discriminator-value 定义插入一个具体的对象的时候,自动向type列中插入指定的值
        -->
        <discriminator column="type" />
        <property name="name" />
        <property name="age" />
        
        <subclass name = "Teacher" discriminator-value="t">
             <property name="job" />
             <property name="merry" />
        </subclass>
        
        <subclass name = "Student" discriminator-value="s">
             <property name="dream" />
             <property name="birthday" />
        </subclass>
    </class>
    

xml(在不同表中):

Person.hbm.xml:
<class name = "Person">
        <id name = "id">
            <generator class="native" />
        </id>
        
        <property name="name" />
        <property name="age" />
        
        <joined-subclass name="Teacher" table="Teacher">
            <key column="pid" />
            <property name="job" />
            <property name="merry" />
        </joined-subclass>
        
        <joined-subclass name="Student" table="Student">
            <key column="pid" />
            <property name="dream" />
            <property name="birthday" />
        </joined-subclass>
        
    </class>

 

Hibernate映射关系配置(五)

标签:pid   ble   XML   对象   nbsp   native   span   student   subclass   

原文地址:http://www.cnblogs.com/hyl-home/p/7577019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!