标签: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; } }
抽象类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; } }
生活照LifeImage.java:
package com.cy.model; public class LifeImage extends Image{ }
工作照WorkImage.java:
package com.cy.model; public class WorkImage extends Image{ }
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>
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>
测试代码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()); } }
三个表的关联关系:
t_lifeimage表结构:
t_lifeimage表数据:
t_workimage表数据:
第二节:根类对应一个表
标签:pac images port e30 技术 system setname pre student
原文地址:http://www.cnblogs.com/tenWood/p/7256112.html