首页 > 其他 > 详细

组件与继承映射

时间:2015-11-30 07:10:29      阅读:316      评论:0      收藏:0      [点我收藏+]

1.组件:component(一个表)

(1)

public class Student
{
    private String id;
    private String name;
    private Address address;
public class Address
{
    private String homeAddress;
    private String schoolAddress;
<hibernate-mapping >
        <class name="com.liule.hibernate.Student" table="student">
            <id name="id" column="id" type="string" >
                <generator class="uuid"></generator> <!--  主键生成方式 -->
            </id>
            
            <property name="name"  type="string">
                <column name="name" length="50"></column>
            </property>
            
            <component name="address" class="com.liule.hibernate.Address">
                <property name="homeAddress" type="string"></property>
                <property name="schoolAddress" type="string"></property>
            </component>
        </class>
</hibernate-mapping>

插入信息:

public static void main(String[] args)
    {
        Student student1 = new Student();
            
        student1.setName("sad");
            
        Address ad = new Address();
        ad.setHomeAddress("shanghai");
        ad.setSchoolAddress("guangzhou");
        
        student1.setAddress(ad);
        
        
        Session session = sf.openSession();
        Transaction tx = null;    
        try
        {

(2)集合类型的组件,本质上于one-to-many相同(不推荐使用)

public class Contact
{
    private String student_id; //新增加的
    private String method;
    private String address;
    
public class Student 
{
    private String id;
    private String name;
    
    private Set contacts = new HashSet();
<hibernate-mapping>
    <class name="com.liule.hibernate.Student" table="Student">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator> <!-- 生成32位的字符串 -->
        </id>        
        <property name="name" column="name" type="string"></property>
        
        <set name="contacts" table="contacts">
            <key column="student_id"></key>
            <composite-element class="com.liule.hibernate.Contact">  <!-- class为组合元素的类的类型 -->
                <property name="address" type="string"></property>
                <property name="method" type="string"></property>
            </composite-element>
        </set>
        
    </class>
</hibernate-mapping>

生成的表:

技术分享

技术分享

2.继承映射(三种方式)

(1)每个子类一张表

public class Person
{
    private String id;
    private String name;
public class Student extends Person
{
    private String cardId;
public class Teacher
{
    private String salary;
<hibernate-mapping>
    <class name="com.liule.hibernate.Student" table="Student">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator> <!-- 生成32位的字符串 -->
        </id>        
        <property name="name" column="name" type="string"></property><!-- 通过get、set方法获得父类的属性 -->
        
        
        <property name="cardId" column="cardId"   type="string"></property>
                
            
        
    </class>
</hibernate-mapping>
<hibernate-mapping>
    <class name="com.liule.hibernate.Teacher" table="Teacher">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator> <!-- 生成32位的字符串 -->
        </id>        
        <property name="name" column="name" type="string"></property><!-- 通过get、set方法获得父类的属性 -->
        
        
                <property name="salary" column="salary"   type="string"></property>
                
            
        
    </class>
</hibernate-mapping

技术分享

技术分享

数据的查询:

public static void main(String[] args)
    {    
        Session session = sf.openSession();
        Transaction ts = null;
        try
        {
            Query query = session.createQuery("from com.liule.hibernate.Person");//多态查询,会将Person内的子类的记录都查询出来.
            //若父类没有hbm文件,必须填写完整的包名
            
            java.util.Iterator it =query.iterate();
            while(it.hasNext())
            {
                Person person = (Person)it.next();
                System.out.println(person.getName());
            }
            
            
            
            ts = session.beginTransaction();
            ts.commit();
        }
        catch(Exception ex)
        {

(2)一张表存储继承体系中所有类的信息(数据库表实际上是继承体系中所有类的属性的并集所构成的字段。)(一张表对应一个hbm文件)

 技术分享

public class Person
{
    private String id;
    private String name;
public class Teacher extends Person
{
    private String salary;
public class Student extends Person
{
    private String cardId;
<hibernate-mapping>
    <class name="com.liule.hibernate.Person" table="person">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator> <!-- 生成32位的字符串 -->
        </id>    
        <!-- 用于区分student、teacher的列 -->
        <discriminator column="personType"  type="string" ></discriminator>
            
        <property name="name" column="name" type="string"></property><!-- 通过get、set方法获得父类的属性 -->
        
        
        
        <subclass name="com.liule.hibernate.Student" discriminator-value="student">
            <property name="cardId"  type="string"></property>
        </subclass>
        <subclass name="com.liule.hibernate.Teacher" discriminator-value="teacher">
            <property name="salary"  type="string"></property>
        </subclass>    
        
    </class>
</hibernate-mapping>

 技术分享

(3)公共信息放在父类表中,独有信息放在子类,每个子类对应一张表

技术分享

<hibernate-mapping>
    <class name="com.liule.hibernate.Person" table="person">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator> <!-- 生成32位的字符串 -->
        </id>    
        <!-- 用于区分student、teacher的列 -->
        
            
        <property name="name" column="name" type="string"></property><!-- 通过get、set方法获得父类的属性 -->
        
        
        
        <joined-subclass name="com.liule.hibernate.Student" table="student">
            <key column="id"></key>
            <property name="cardId" type="string"></property>
        </joined-subclass>
        
        <joined-subclass name="com.liule.hibernate.Teacher" table="teacher">
            <key column="id"></key>
            <property name="salary" type="string"></property>
        </joined-subclass>
        
    </class>
</hibernate-mapping>

 

组件与继承映射

原文:http://www.cnblogs.com/liu-Gray/p/5006113.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!