Hibernate学习7—Hibernate 映射继承

技术分享
package com.cy.model;

import java.util.Set;

public class Student {
    private int id;
    private String name;
    private SetImage 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 SetImage getImages() {
        return images;
    }
    public void setImages(SetImage 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(){
        ListImage imageList = new ArrayListImage();
        int stuId = 1;
        ListImage lifeImageList = (ListImage)session.createQuery("from LifeImage l where l.student.id = " + stuId).list();
        imageList.addAll(lifeImageList);
        ListImage workImageList = (ListImage)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 映射继承

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


最新回复(0)
/jishuibG7nzo4WE_2BP_2FSzC5PCauiHHGR00nGQg4nru9A_3D_3D4719392
8 简首页