需求:学生有很多照片,分为生活照和工作照;
第一节:每个具体类对应一个表
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表数据:
第二节:根类对应一个表
Student2.java:
package com.cy.model; import java.util.Set; public class Student2 { private int id; private String name; private Set<Image2> 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<Image2> getImages() { return images; } public void setImages(Set<Image2> images) { this.images = images; } }
Image2.java:
package com.cy.model; public class Image2 { private int id; private String imageType; private String imageName; private Student2 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 Student2 getStudent() { return student; } public void setStudent(Student2 student) { this.student = student; } public String getImageType() { return imageType; } public void setImageType(String imageType) { this.imageType = imageType; } }
LifeImage2.java:
package com.cy.model; public class LifeImage2 extends Image2{ }
WorkImage2.java:
package com.cy.model; public class WorkImage2 extends Image2{ }
Student2.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student2" table="t_student2"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <set name="images"> <key column="stuId"></key> <one-to-many class="com.cy.model.Image2"/> </set> </class> </hibernate-mapping>
Image2.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Image2" table="t_image2"> <id name="id" column="imageId"> <generator class="native"></generator> </id> <!-- 用来区分是LifeImage2还是WorkImage2 --> <discriminator column="imageType" type="string"></discriminator> <property name="imageName" column="imageName"></property> <many-to-one name="student" column="stuId" class="com.cy.model.Student2"></many-to-one> <subclass name="com.cy.model.LifeImage2" discriminator-value="life"></subclass> <subclass name="com.cy.model.WorkImage2" discriminator-value="work"></subclass> </class> </hibernate-mapping>
hibernate.cfg.xml中只需加入两项映射配置就行了:
<mapping resource="com/cy/model/Student2.hbm.xml"/> <mapping resource="com/cy/model/Image2.hbm.xml"/>
测试代码:
@Test public void testGetAllImages2(){ Student2 s2 = (Student2) session.get(Student2.class, 1); Set<Image2> images = s2.getImages(); Iterator<Image2> it = images.iterator(); while(it.hasNext()){ Image2 image = it.next(); System.out.println(image.getImageName()); } }
生成的t_image2表结构:
第三节:每个类对应一个表
Student3.java:
package com.cy.model; import java.util.Set; public class Student3 { private int id; private String name; private Set<Image3> 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<Image3> getImages() { return images; } public void setImages(Set<Image3> images) { this.images = images; } }
Image3.java:
package com.cy.model; public class Image3 { private int id; private String imageName; private Student3 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 Student3 getStudent() { return student; } public void setStudent(Student3 student) { this.student = student; } }
WorkImage3.java:
package com.cy.model; public class WorkImage3 extends Image3{ }
LifeImage3.java:
package com.cy.model; public class LifeImage3 extends Image3{ }
Student3.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cy.model"> <class name="Student3" table="t_student3"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <set name="images"> <key column="stuId"></key> <one-to-many class="com.cy.model.Image3"/> </set> </class> </hibernate-mapping>
Image3.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Image3" table="t_image3"> <id name="id" column="imageId"> <generator class="native"></generator> </id> <property name="imageName" column="imageName"></property> <many-to-one name="student" column="stuId" class="com.cy.model.Student3"></many-to-one> <joined-subclass name="com.cy.model.LifeImage3" table="t_lifeImage3"> <key column="lifeImageId"></key> </joined-subclass> <joined-subclass name="com.cy.model.WorkImage3" table="t_workImage3"> <key column="workImageId"></key> </joined-subclass> </class> </hibernate-mapping>
hibernate.cfg.xml中加入如下映射关系:
<mapping resource="com/cy/model/Student3.hbm.xml"/> <mapping resource="com/cy/model/Image3.hbm.xml"/>
测试代码:
@Test public void testGetAllImages3(){ Student3 s3 = (Student3) session.get(Student3.class, 1); Set<Image3> images = s3.getImages(); Iterator<Image3> it = images.iterator(); while(it.hasNext()){ Image3 image = it.next(); System.out.println(image.getImageName()); } }
查看生成的表:
t_image3的表结构: