Hibernate4自学入门(九)——映射继承

时间:2021-09-17 07:37:48


一、每个具体类对应一个表

model

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;
}


}

一个抽象的父类

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;
}





}

多个继承上述抽象父类的子类:

public class LifeImage extends Image {



}


每个实体类对应一个表 即:student、workImage、LifeImage各生成一张表


<hibernate-mapping package="com.gw.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.gw.model.Student"></many-to-one>

</class>

</hibernate-mapping>
LifeImage同上、Student即标准写法。


test:


@Test
public void test() throws IOException, ParseException {
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);
Iterator it=imageList.iterator();

while(it.hasNext()){

Image image =(Image)it.next();
System.out.println(image.getImageName());


}


}



二、根类对应一个表

student 不变。


根类Image:


public  class Image2 {
private int id;
private String imageName;
private Student2 student;
private String imageType;

public String getImageType() {
return imageType;
}
public void setImageType(String imageType) {
this.imageType = imageType;
}
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;
}






}

概念:imageType 用来给表里的信息进行分类提取——对应下述的discriminator。


根类对应的表:


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

<class name="Image2" table="t_image2">
<id name="id" column="imageId">
<generator class="native"></generator>
</id>
<discriminator column="imageType" type="string"></discriminator>
<property name="imageName" column="imageName"></property>

<many-to-one name="student" column="stuId" class="com.gw.model.Student2"></many-to-one>

<subclass name="com.gw.model.LifeImage2" discriminator-value="li"></subclass>
<subclass name="com.gw.model.WorkImage2" discriminator-value="wo"></subclass>
</class>

</hibernate-mapping>

Student对应的表


<hibernate-mapping package="com.gw.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.gw.model.Image2"/>
</set>
</class>

</hibernate-mapping>

test:

@Test
public void test2() throws IOException, ParseException {
Student2 student2 = (Student2) session.get(Student2.class, 1);

Set<Image2> images=student2.getImages();


Iterator it=images.iterator();

while(it.hasNext()){

Image2 image =(Image2)it.next();
System.out.println(image.getImageName());


}


}


三、每个类对应一个表

image根类和Student model类不变 两个子类不变

Student对应的表:

<hibernate-mapping package="com.gw.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.gw.model.Image3"/>
</set>
</class>

</hibernate-mapping>


Image对应的表:

<hibernate-mapping package="com.gw.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.gw.model.Student3"></many-to-one>

<joined-subclass name="com.gw.model.LifeImage3" table="t_lifeImage3">
<key column="lifeImageId"></key>
</joined-subclass>

<joined-subclass name="com.gw.model.WorkImage3" table="t_workImage3">
<key column="workImageId"></key>
</joined-subclass>
</class>

</hibernate-mapping>