当前位置:编程学习 > JAVA >>

Hibernate继承映射

首先是:数据库表设计

然后实体类中的Person类
[java] 
public class Person { 
    protected Integer id; 
    protected String name; 
    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; 
    } 

学生继承Person。
[java]
public class Student extends Person { 
    private String school; 
 
    public String getSchool() { 
        return school; 
    } 
 
    public void setSchool(String school) { 
        this.school = school; 
    } 

然后Worker继承Person
[java] 
public class Worker extends Person{ 
    private String factory; 
 
    public String getFactory() { 
        return factory; 
    } 
 
    public void setFactory(String factory) { 
        this.factory = factory; 
    } 

然后配置Person.hbm.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
 
<hibernate-mapping package="entity"> 
    <class name="Person" table="person"> 
        <id name="id" type="java.lang.Integer" column="id"> 
            <generator class="native"></generator> 
        </id> 
        <!-- 标志列,判断是student还是worker的列 --> 
        <discriminator column="type" type="string"></discriminator> 
        <property name="name" column="name" type="string"></property> 
        <!-- Student子类  并设置student的标志是student--> 
        <subclass name="Student" discriminator-value="student"> 
            <property name="school" column="school" type="string"></property> 
        </subclass> 
        <!-- Worker子类 并设置worker的标志是worker--> 
        <subclass name="Worker" discriminator-value="worker"> 
            <property name="factory" column="factory" type="string"></property> 
        </subclass> 
    </class> 
</hibernate-mapping> 
然后测试类
[java] 
package Run; 
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
 
import entity.Student; 
import entity.Worker; 
 
public class Run{ 
    Session session=null; 
    SessionFactory sf=null; 
     
    public static void main(String[] args) { 
        new Run().test1(); 
    } 
    public void test1(){ 
        Student s1=new Student(); 
        s1.setName("新色"); 
        s1.setSchool("海淀中学"); 
        Worker w1=new Worker(); 
        w1.setName("老张2号"); 
        w1.setFactory("新水网"); 
         
        try { 
            session=new Configuration().configure().buildSessionFactory().openSession(); 
            session.beginTransaction(); 
            session.save(s1); 
            session.save(w1); 
            session.beginTransaction().commit(); 
        } catch (HibernateException e) { 
 
            e.printStackTrace(); 
            session.beginTransaction().rollback(); 
        } 
        finally{ 
            if(session!=null) 
                session.close(); 
        } 
    } 
     

执行后控制台输出
[java] 
Hibernate: insert into person (name, school, type) values (?, ?, 'student') 
Hibernate: insert into person (name, factory, type) values (?, ?, 'worker') 

再看数据库结果

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,