Hibernate 继承 – 根类映射成一个表

2023-09-24 22 0

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;}@Overridepublic 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;}@Overridepublic 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)
代码编程
赞赏

相关文章

学3D建模一定要有美术功底?有则锦上添花,没有也不影响你学习
3D建模难学吗?零基础学6个月,能否找到工作?
3D建模入行难不难?零基础学习次世代,这6条建议能帮到你
游戏建模师薪资能给到多少?建模初学者如何快速入行,获取涨薪机会?
游戏建模行业门槛真有这么高?教你如何学习,找到合适自己的方式
关于3D建模的行业状况,薪资和前景如何,小白该如何入门?