Hibernate 繼承 - 每個類都對映成一個類
阿新 • • 發佈:2019-01-13
Hibernate 繼承 - 每個類都對映成一個類
- Image.java
package com.java1234.hibernate.model; public class Image { private int id; private String name; //建立student物件,用於對映student資訊。 private Student student; 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 Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @Override public String toString() { return "Image [id=" + id + ", name=" + name + ", student=" + student + "]"; } }
- LifeImages.java
package com.java1234.hibernate.model; public class LifeImages extends Image{ private int id; private String name; //建立student物件,用於對映student資訊。 private Student student; 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 Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
- WorkImages.java
package com.java1234.hibernate.model; public class WorkImages extends Image{ private int id; private String name; //建立student物件,用於對映student資訊。 private Student student; 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 Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
- Student.java
package com.java1234.hibernate.model;
import java.util.Set;
public class Student {
private int id;
private String name;
private Set<Image> images;
public Set<Image> getImages() {
return images;
}
public void setImages(Set<Image> images) {
this.images = 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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
- image.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.java1234.hibernate.model.Image" table="tb_image">
<id name="id" column="imgId">
<generator class="native"/>
</id>
<property name="name" column="imgName"/>
<many-to-one name="student" class="com.java1234.hibernate.model.Student" column="stuId" cascade="save-update"/>
<joined-subclass name="com.java1234.hibernate.model.LifeImages" table="tb_lifeImage">
<key column="lifeImageId"/>
</joined-subclass>
<joined-subclass name="com.java1234.hibernate.model.WorkImages" table="tb_workImage">
<key column="workImageId"/>
</joined-subclass>
</class>
</hibernate-mapping>
- student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.java1234.hibernate.model.Student" table="tb_student" lazy="false">
<id name="id" column="stuId">
<generator class="native"/>
</id>
<property name="name" column="stuName"/>
<!-- 由於Image.java是一個非抽象的類,所以可以通過Image類來例項化物件。
在這裡 通過配置set,用於配置雙向的一對多對映 -->
<set name="images" table="tb_image" cascade="save-update">
<key column="stuId"/><!-- 通過image表裡的stuId完成連線 -->
<one-to-many class="com.java1234.hibernate.model.Image"/>
</set>
</class>
</hibernate-mapping>
生成的資料庫表結構/執行過程中的SQL
Hibernate: create table tb_image (imgId integer not null auto_increment, imgName varchar(255), stuId integer, primary key (imgId))
Hibernate: create table tb_lifeImage (lifeImageId integer not null, primary key (lifeImageId))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: create table tb_workImage (workImageId integer not null, primary key (workImageId))
Hibernate: alter table tb_image add constraint FK_8f53143v9q9i7j3mgk7b090pk foreign key (stuId) references tb_student (stuId)
Hibernate: alter table tb_lifeImage add constraint FK_6ieihsxe00srdc2nqhc9i2lpw foreign key (lifeImageId) references tb_image (imgId)
Hibernate: alter table tb_workImage add constraint FK_fpcoksmnfjchh5rth9a5sk47x foreign key (workImageId) references tb_image (imgId)