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

Hibernate 繼承 - 每個實現類對映成一個表

Hibernate 繼承

類與類之間的繼承是Java的絕對核心,hibernate作為一個用於ORM框架,將類與類之間的繼承對映成表的關係,也是hibernate所支援的功能之一。
實際上在hibernate中有三種方式支援這一特性。
1. 每個實現類對映成一個表
2. 根類對映成一個表
3. 每個類都對映成一個類

為了描述這種關係,工作圖片和生活圖片都繼承自圖片類/介面,每個圖片都屬於一個學生,實現:查詢一個學生下所有的圖片。

每個實現類對映成一個表

- Image.java 抽象類

package com.java1234.hibernate.model;

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

- 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 + "]";
	}
	
}

- lifeImage.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.LifeImages" table="tb_lifeImg">
    		<id name="id" column="lifeImgId">
    			<generator class="native"/>
    		</id>
    		<property name="name" column="lifeImgName"/>
			
			<many-to-one name="student" class="com.java1234.hibernate.model.Student" column="stuId" cascade="save-update"/>
    	</class>
    
    </hibernate-mapping>

- workImage.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.LifeImages" table="tb_lifeImg">
    		<id name="id" column="lifeImgId">
    			<generator class="native"/>
    		</id>
    		<property name="name" column="lifeImgName"/>
			
			<many-to-one name="student" class="com.java1234.hibernate.model.Student" column="stuId" cascade="save-update"/>
    	</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">
    		<id name="id" column="stuId">
    			<generator class="native"/>
    		</id>
    		<property name="name" column="stuName"/>

    	</class>
    
    </hibernate-mapping>

生成的表結構/SQL

Hibernate: create table tb_lifeImg (lifeImgId integer not null auto_increment, lifeImgName varchar(255), stuId integer, primary key (lifeImgId))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: create table tb_workImg (workImgId integer not null auto_increment, workImgName varchar(255), stuId integer, primary key (workImgId))
Hibernate: alter table tb_lifeImg add constraint FK_cb4uw507aecbenhaihjrythw foreign key (stuId) references tb_student (stuId)
Hibernate: alter table tb_workImg add constraint FK_kjo52qqv4vm5fulws5xfhb0fd foreign key (stuId) references tb_student (stuId)

插入

執行時SQL: Hibernate: insert into tb_student (stuName) values (?)

   

//由於Image是一個抽象類,所以在studnet一側無法配置set以實現一對多關係。在插入/查詢Student時無法插入/查詢出對應的圖片資訊
	@Test
	public void testInsertStudnet(){
		session.beginTransaction();
		Student student = new Student();
		student.setName("Robin");
		Image image1 = new WorkImages();
		image1.setName("Office view");
		Image image2 = new WorkImages();
		image2.setName("WorkStation view");
		Image image3 = new LifeImages();
		image3.setName("ThanksGiving Day");
		
		Set<Image> images = new HashSet<Image>();
		images.add(image1);
		images.add(image2);
		images.add(image3);
		student.setImages(images);
		session.save(student);
		session.getTransaction().commit();
	}

如果通過反向,則會級聯儲存成功。

        @Test
	public void testInsertImages(){
		session.beginTransaction();
		Student student1 = new Student();
		student1.setName("Robin");
		Student student2 = new Student();
		student2.setName("Mary");

		Image image1 = new WorkImages();
		image1.setName("Office view");
		Image image2 = new WorkImages();
		image2.setName("WorkStation view");
		Image image3 = new LifeImages();
		image3.setName("ThanksGiving Day");
		
		image1.setStudent(student1);
		image2.setStudent(student2);
		image3.setStudent(student1);
		
		session.save(image1);
		session.save(image2);
		session.save(image3);
		session.getTransaction().commit();
	}