Hibernate的继承关系

开发 后端
本文讲述Hibernate的继承关系。Hibernate继承关系分为三种:一张表对应一整棵类继承树、一个类对应一张表、每一个具体类对应一张表。

Hibernate的继承关系分为三种:一张表对应一整棵类继承树、一个类对应一张表、每一个具体类对应一张表。

三个java类Person、Student、Teacher,Student和Teacher继承Person类并且Student和Teacher没有任何关系

代码如下:

Person.java

package com.zzn.hibernate.model;  
 
public class Person {  
    private int id;  
    private String name;  
    private int age;  
    public int getId() {  
        return id;  
    }  
    public void setId(int 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;  
    }  
      
      
}      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

Student.java

package com.zzn.hibernate.model;  
 
public class Student extends Person {  
    private String cardId;  
 
    public String getCardId() {  
        return cardId;  
    }  
    public void setCardId(String cardId) {  
        this.cardId = cardId;  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Teacher.java

package com.zzn.hibernate.model;  
 
public class Teacher extends Person {  
    private int salary;  
    public int getSalary() {  
        return salary;  
    }  
    public void setSalary(int salary) {  
        this.salary = salary;  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Hibernate的继承关系表:一张表对应一整棵类继承树时

Person.hbm.xml

< ?xml version="1.0" encoding='gb2312'?>    
< !DOCTYPE hibernate-mapping PUBLIC    
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
       
< hibernate-mapping package="com.zzn.hibernate.model"> 
 
    < class name="Person" table="person"> 
        < id name="id" column="id"> 
            < generator class="identity" /> 
        < /id> 
        < discriminator column="personType" type="java.lang.String" />   < !-- 指定描述符对应的列明,注意discrimination标签只能紧跟在id标签后面 --> 
          
        < property name="name" type="java.lang.String" />                < !-- 公用的name属性,被两个sbuclass标签所用 --> 
        < property name="age" type="java.lang.Integer" />                < !-- 公用的age属性,被两个sbuclass标签所用 --> 
          
        < subclass name="Student" discriminator-value="student">            < !-- 映射子类Student,描述符是student,此时已经映射Student类,所以不必再写Student.hbm.xml --> 
            < property name="cardId" type="java.lang.String" />            < !-- Student私有的属性 --> 
        < /subclass> 
        < subclass name="Teacher" discriminator-value="teacher" > 
            < property name="salary" type="java.lang.Integer" />            < !-- Teacher私有的属性 --> 
        < /subclass> 
              
    < /class> 
      
< /hibernate-mapping> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

一个类对应一张表

< ?xml version="1.0" encoding='gb2312'?>    
< !DOCTYPE hibernate-mapping PUBLIC    
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
       
< hibernate-mapping package="com.zzn.hibernate.model"> 
 
    < class name="Person" table="person"> 
        < id name="id" column="id"> 
            < generator class="identity" /> 
        < /id> 
        < property name="name" type="java.lang.String" />                  
        < property name="age" type="java.lang.Integer" /> 
        < joined-subclass name="Student" table="student"> 
            < key column="id" /> 
            < property name="cardId" type="java.lang.String" /> 
        < /joined-subclass> 
          
        < joined-subclass name="Teacher" table="teacher"> 
            < key column="id"/> 
            < property name="salary" type="java.lang.Integer" /> 
        < /joined-subclass> 
    < /class> 
      
< /hibernate-mapping> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

hibernate.hbm.xml

< ?xml version='1.0' encoding='UTF-8'?> 
< !DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
< hibernate-configuration> 
    < session-factory> 
        < property name="show_sql">true< /property> 
        < property name="hibernate.hbm2ddl.auto">update< /property> 
        < property name="connection.username">sa< /property> 
        < property name="connection.password">< /property> 
        < property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;databasename=hibernate_test< /property> 
        < property name="dialect">org.hibernate.dialect.SQLServerDialect< /property> 
        < property name="myeclipse.connection.profile">SQL2005< /property> 
        < property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver< /property> 
        < mapping resource="com/zzn/hibernate/model/Person.hbm.xml"/> 
    < /session-factory> 
< /hibernate-configuration> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

Hibernate的继承关系测试文件SubclassTest.java

package com.zzn.hibernate.test;  
 
import java.util.Iterator;  
 
import org.hibernate.Query;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  
 
import com.zzn.hibernate.model.Person;  
import com.zzn.hibernate.model.Student;  
import com.zzn.hibernate.model.Teacher;  
 
public class SubClassTest {  
    public static void main(String[] args) {  
        add();  
        select();  
    }  
 
    public static void add() {  
        Configuration configuration = null;  
        SessionFactory sessionFactory = null;  
        Session session = null;  
        Transaction transaction = null;  
        Student student = new Student();  
        Teacher teacher = new Teacher();  
        try {  
            configuration = new Configuration();  
            sessionFactory = configuration.configure().buildSessionFactory();  
            session = sessionFactory.openSession();  
            transaction = session.beginTransaction();  
            student.setName("shanshan");  
            student.setAge(25);  
            student.setCardId("123456");  
            teacher.setName("xiaolan");  
            teacher.setAge(25);  
            teacher.setSalary(5000);  
            session.save(student);  
            session.save(teacher);  
        } finally {  
            if (session != null) {  
                transaction.commit();  
                session.close();  
            }  
        }  
    }  
      
    public static void select(){  
        Configuration configuration = null;  
        SessionFactory sessionFactory = null;  
        Session session = null;  
        try {  
            configuration = new Configuration();  
            sessionFactory = configuration.configure().buildSessionFactory();  
            session = sessionFactory.openSession();  
            Query query = session.createQuery("from Person");  
            Iterator iterator = query.list().iterator();  
            while (iterator.hasNext()){  
                Person person = (Person)iterator.next();  
                System.out.println(person.getName());  
            }  
        } finally {  
            if (session != null) {  
                session.close();  
            }  
        }  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.

【编辑推荐】

  1. 有关Hibernate延时加载与lazy机制
  2. Hibernate访问多个数据库
  3. Hibernate的lazy属性总结
  4. Hibernate中hbm的generator子元素
  5. 简单理解Hibernate三种状态的概念及互相转化
责任编辑:book05 来源: javablog
相关推荐

2012-05-30 15:03:43

ibmdw

2009-09-25 14:12:16

Hibernate继承

2012-02-02 16:13:29

HibernateJava

2009-06-16 14:36:54

Hibernate继承

2009-09-25 14:20:28

Hibernate继承映射

2012-02-03 10:54:50

HibernateJava

2009-09-25 15:34:42

Hibernate关联

2009-06-02 14:46:26

Hibernate关系映射教程

2009-09-23 13:26:10

Hibernate对象

2009-09-25 12:59:52

Hibernate映射

2017-02-06 13:31:19

Android样式继承关系

2009-06-04 10:34:19

Hibernate一对一对多关系配置

2009-06-17 15:52:23

Hibernate查询

2009-06-18 14:22:06

Hibernate多对Hibernate

2012-02-08 12:17:38

HibernateJava

2009-06-03 16:27:27

Hibernate一对一关系

2011-08-08 09:51:52

Cocoa 框架

2010-01-19 18:51:17

C++类

2012-02-08 13:34:08

HibernateJava

2013-03-04 11:10:03

JavaJVM
点赞
收藏

51CTO技术栈公众号