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

Hibernate学习7—Hibernate 映射继承

时间:2017-07-29 16:44:46      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:pac   images   port   e30   技术   system   setname   pre   student   

需求:学生有很多照片,分为生活照和工作照;

第一节:每个具体类对应一个表                        

Student.java:

技术分享
package com.cy.model;

import java.util.Set;

public class Student {
    private int id;
    private String name;
    private Set<Image> images;
    
    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 Set<Image> getImages() {
        return images;
    }
    public void setImages(Set<Image> images) {
        this.images = images;
    }
    
    
    
}
View Code

抽象类Image.java:

技术分享
package com.cy.model;

public abstract class Image {
    private int id;
    private String imageName;
    private Student student;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getImageName() {
        return imageName;
    }
    public void setImageName(String imageName) {
        this.imageName = imageName;
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    
    
}
View Code

生活照LifeImage.java:

技术分享
package com.cy.model;

public class LifeImage extends Image{

}
View Code

工作照WorkImage.java:

技术分享
package com.cy.model;

public class WorkImage extends Image{

}
View Code

Student.hbm.xml:

<hibernate-mapping package="com.cy.model">

    <class name="Student" table="t_student">
        <id name="id" column="stuId">
            <generator class="native"></generator>
        </id>
        <property name="name" column="stuName"></property>
    </class>

</hibernate-mapping>

LifeImage.hbm.xml:

技术分享
<hibernate-mapping package="com.cy.model">

    <class name="LifeImage" table="t_lifeImage">
        <id name="id" column="lifeImageId">
            <generator class="native"></generator>
        </id>
        <property name="imageName" column="imageName"></property>
        
        <many-to-one name="student" column="stuId" class="com.cy.model.Student"></many-to-one>
    </class>

</hibernate-mapping>
View Code

WorkImage.hbm.xml:

技术分享
<hibernate-mapping package="com.cy.model">

    <class name="WorkImage" table="t_workImage">
        <id name="id" column="workImageId">
            <generator class="native"></generator>
        </id>
        <property name="imageName" column="imageName"></property>
        
        <many-to-one name="student" column="stuId" class="com.cy.model.Student"></many-to-one>
    </class>

</hibernate-mapping>
View Code

测试代码StudentTest.java:

技术分享
    @Test
    public void testGetAllImages(){
        List<Image> imageList = new ArrayList<Image>();
        int stuId = 1;
        List<Image> lifeImageList = (List<Image>)session.createQuery("from LifeImage l where l.student.id = " + stuId).list();
        imageList.addAll(lifeImageList);
        List<Image> workImageList = (List<Image>)session.createQuery("from WorkImage w where w.student.id = " + stuId).list();
        imageList.addAll(workImageList);
        for(Image image: imageList){
            System.out.println(image.getImageName());
        }
        
    }
View Code

三个表的关联关系:

技术分享

t_lifeimage表结构:

技术分享

t_lifeimage表数据:

技术分享

t_workimage表数据:

技术分享

 

 

第二节:根类对应一个表                                      

 

Hibernate学习7—Hibernate 映射继承

标签:pac   images   port   e30   技术   system   setname   pre   student   

原文地址:http://www.cnblogs.com/tenWood/p/7256112.html

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