Hibernate继承(1)单表继承

Hibernate继承(1)单表继承

代码:

package bean;

import java.util.Date;

public class Person {

    private Integer id;
    private String name;
    private int password;
    private Date birthday;
    
    public Person()
    {
        
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", password=" + password
                + ", birthday=" + birthday + "]";
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
    
    
    
}
package bean;

public class Student extends Person{

	private int classId;
	private String className;
	private int course;
	@Override
	public String toString() {
		return "Student [classId=" + classId + ", className=" + className
				+ ", course=" + course + ", getId()=" + getId()
				+ ", getName()=" + getName() + ", getPassword()="
				+ getPassword() + ", getBirthday()=" + getBirthday() + "]";
	}
	public int getClassId() {
		return classId;
	}
	public void setClassId(int classId) {
		this.classId = classId;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public int getCourse() {
		return course;
	}
	public void setCourse(int course) {
		this.course = course;
	}
	
	
	
}

  

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<!-- 单表继承关系  配置实现 
    因为是多个子类都存储在 同一张表中,为了区分不同的子类,单表继承策略使用了一个 辨别列(discriminator)
    注意:discriminator 辨别列一定要定义在 id 后面,紧跟id配置
    type="string" 指定辨别列类型  column="type_id" 辨别列名称
    
    通过subclass引入 子类
    
 -->
<hibernate-mapping package="bean">
    <class name="Person" table="t_person" >
        <id name="id"><generator class="identity"/></id>
        <discriminator type="string" column="type_id"/>
        <property name="name"/>
        <property name="password"/>
        <property name="birthday"/>
        
        <subclass name="Student">
            <property name="classId"/>
            <property name="className"/>
            <property name="course"/>
        </subclass>

    </class>
    
</hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 显示执行SQL语句 -->
        <property name="show_sql">true</property>
         <!-- 格式化SQL语句 -->
        <property name="format_sql">false</property>
        <!-- 驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库url-->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property>
        <!-- 访问数据库用户名 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 访问数据库密码 -->
        <property name="hibernate.connection.password">root</property>
        <!-- 方言(为了更好的操作具体的数据库) 
            如果使用的mysql数据版本在5.5之后的话,方言通常建议使用MySQL5InnoDBDialect
            MySQLDialect 不支持事务
            MySQLInnoDBDialect 支持事务
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 自动创建表结构配置
            create-drop 在程序启动的时候创建对应数据库表结构,当SessionFatory关闭的时候会将创建的表结构删除
            create 在每次程序启动的时候先删除上次创建的数据库表结构,然后再创建对应新数据库表结构。
            update 在每次启动的时候会追加修改的表结构,但是不会影响原来的数据 (通常用这个)
            validate 在每次启动的时候会验证并修改的表结构。
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- 引入对应的需要持久化类的配置文件 -->
        <mapping resource="bean/Person.hbm.xml"/>
        
    </session-factory>
</hibernate-configuration>
package Test;



import bean.Person;
import bean.Student;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


public class HibernateTest {
    SessionFactory  sessionFactory =null;
    Session session =null;
    Transaction tx = null;
    /**
     * 初始化测试数据 
     * @throws Exception
     */

    public void setUp() throws Exception {
        System.out.println("------setUp---初始化测试资源-----");
        Configuration config = new Configuration().configure();
        ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        sessionFactory = config.buildSessionFactory(sr);
        session = sessionFactory.openSession();
    }
    
    
    public void testAdd(){
        Person p = new Person();
        p.setName("tom");
        p.setPassword(123456);
        p.setBirthday(new java.util.Date());

        tx = session.beginTransaction();
    
        session.persist(p);
        tx.commit();
    }
    
    
    public void testAdd2(){
        Student stu = new Student();
        stu.setName("stu");
        stu.setPassword(123456);
        stu.setBirthday(new java.util.Date());
        stu.setClassId(1206);
        stu.setClassName("J1206");
        stu.setCourse(90);
        tx = session.beginTransaction();
    
        session.persist(stu);
        tx.commit();
    }
    
    
    public void testGet(){
        
        Person p = (Person)session.get(Person.class, 1);
        
        System.out.println(p);
        
    }
    
    public void testGet2(){
        
        Student stu = (Student)session.get(Student.class, 2);
        
        System.out.println(stu);
        
    }
    
    
    /**
     * 释放测试数据 
     * @throws Exception
     */
    
    public void tearDown() throws Exception {
        System.out.println("------tearDown---释放测试数据---");
        session.close();
        sessionFactory.close();
    }
    
    public static void main(String [] args) throws Exception
    {
        HibernateTest h = new HibernateTest();
        h.setUp();
        h.testAdd2();
        h.tearDown();
    }

}

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。