1. 程式人生 > >Hibernate 繼承 - 根類對映成一個表

Hibernate 繼承 - 根類對映成一個表

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>
    		
    		
    		<!-- 在hibernate生成的image表中,會多加一列imageType。把所有圖片都儲存到一個表裡,通過imgType屬性來標識 -->
    		<!-- 注意string並非java中的String,必須小寫。並且此元素必須緊跟主鍵宣告之後 -->
    		<discriminator column="imgType" type="string"></discriminator>
    		
    		
    		<property name="name" column="imgName"/>
    		<many-to-one name="student" class="com.java1234.hibernate.model.Student" column="stuId" cascade="save-update"/>
    		
    		<!-- 兩個實現類分別宣告自己的shortName,在存入資料庫的時候,hibenrate會區分例項化物件的類,並匹配別名後儲存到表中 -->
    		<!-- 同樣注意subclass在class之中的位置,必須置於property,many-to-one之後 -->
    		<subclass name="com.java1234.hibernate.model.LifeImages" discriminator-value="lI"/>
			<subclass name="com.java1234.hibernate.model.WorkImages" discriminator-value="wI"/>
			
    	</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, imgType varchar(255) not null, imgName varchar(255), stuId integer, primary key (imgId))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: alter table tb_image add constraint FK_8f53143v9q9i7j3mgk7b090pk foreign key (stuId) references tb_student (stuId)