1.概述
关系数据库的表之间不存在继承关系,为了将面向对象中的继承关系映射到关系数据库中,可使用三种映射策略:
1、每个继承层次一张表
2、每个具体类一张表
3、每个类一张表
2.每个继承层次一张表
即用一张表表示一个继承层次,因此需要增加一个字段用于区别
public class Person {
private int id;
private String name;
private String sex;
private int age;
}
public class Student extends Person {
private String sno;
private String school;
}
public class Worker extends Person{
private String no;
private double salary;
}
映射语法:
1、定义一个以父类命名的映射文件
2、<id>标记对后面添加<discriminator>标记,并指定column属性来定义鉴别字段
3、父类属性放在<class>标记对之间,子类属性放在<subclass>标记对之间
4、在各类标记中使用discriminator-value属性指定各类对应的鉴别字段的值
示例:
1、数据库连接
2、为项目添加Hibernate支持
3、创建持久化类
4、创建映射文件
5、修改hibernate配置文件
6、创建工具类获取Session实例
7、编写Dao接口及其实现类
8、测试
项目结构:
Person类:
public class Person {
private int id;
private String name;
private String sex;
private int age;
public Person() {}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Worker类:
public class Worker extends Person{
private String no;
private double salary;
public Worker() {}
public Worker(String name, String sex, int age, String no, double salary) {
super(name, sex, age);
this.no = no;
this.salary = salary;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Student类:
public class Student extends Person {
private String sno;
private String school;
public Student() {
}
public Student(String name, String sex, int age, String sno, String school) {
super(name, sex, age);
this.sno = sno;
this.school = school;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
工具类:
public class HibernateUtil {
private static SessionFactory sf;
// 工具类不允许实例化
private HibernateUtil() {
}
// 静态代码块,只执行一次,提高性能
static {
Configuration cf = new Configuration().configure();
sf = cf.buildSessionFactory();
}
public static Session getSession() {
Session session = sf.getCurrentSession();
return session;
}
}
Dao及其实现类:
public interface HibernateDao {
public void addPerson(Person person);
@SuppressWarnings("unchecked")
public List getAllPerson(String hql);
}
public class HibernateDaoImpl implements HibernateDao {
@Override
public void addPerson(Person person) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(person);
tx.commit();
} catch (Exception e) {
if (null != tx) {
tx.rollback();
}
e.printStackTrace();
}
}
@Override
@SuppressWarnings("unchecked")
public List getAllPerson(String hql) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(hql);
List persons = query.list();
tx.commit();
return persons;
}
}
测试类:
public class HibernateTest {
public static void main(String[] args) {
Person person = new Person("张三", "男", 20);
Worker worker = new Worker("李四", "女", 28, "P0001", 6000);
Student student = new Student("王五", "女", 19, "SD001", "广金");
HibernateDao hibernateDao = new HibernateDaoImpl();
hibernateDao.addPerson(person);
hibernateDao.addPerson(student);
hibernateDao.addPerson(worker);
String hql = "from Person";
List<Person> persons = hibernateDao.getAllPerson(hql);
System.out.println("========所有人员信息=======");
for (Person p : persons) {
System.out.println("姓名:" + p.getName() + " 性别:" + p.getSex() + " 年龄:" + p.getAge());
}
hql = "from Worker";
List<Worker> workers = hibernateDao.getAllPerson(hql);
System.out.println("========工人信息=======");
for (Worker w : workers) {
System.out.println("姓名:" + w.getName() + " 性别:" + w.getSex() + " 年龄:" + w.getAge() +
" 工号:" + w.getNo() + "薪水:" + w.getSalary());
}
hql = "from Student";
List<Student> students = hibernateDao.getAllPerson(hql);
System.out.println("========学生信息=======");
for (Student s : students) {
System.out.println("姓名:" + s.getName() + " 性别:" + s.getSex() + " 年龄:" + s.getAge() +
" 学号:" + s.getSno() + "学校:" + s.getSchool());
}
}
}
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="这里写你的Person类所在的包">
<!--ORM对象、关系数据库映射-->
<class name="Person" table="person" discriminator-value="person">
<id name="id" type="integer">
<generator class="native"/>
</id>
<!--设置鉴别字段-->
<discriminator column="type" type="string" length="15"/>
<!--从父类继承下来的共享属性-->
<property name="name" type="string" column="username" length="12" not-null="true"/>
<property name="sex" type="string" length="1"/>
<property name="age" type="integer"/>
<subclass name="Student" discriminator-value="student">
<!--子类新增属性-->
<property name="sno" type="string" length="10"/>
<property name="school" type="string" length="50"/>
</subclass>
<subclass name="Worker" discriminator-value="worker">
<!--子类新增属性-->
<property name="no" column="wno" type="string" length="10"/>
<property name="salary" type="double"/>
</subclass>
</class>
</hibernate-mapping>
hibernate配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useSSL=true</property>
<property name="connection.username">root</property>
<property name="connection.password">这里填你的密码</property>
<!--连接池设置-->
<property name="connection.pool_size">2</property>
<!--数据库方言设置-->
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!--向控制台显示执行的SQL语句-->
<property name="show_sql">false</property>
<!--格式化SQL语句后输出-->
<property name="hibernate.format_sql">true</property>
<!--事务配置-->
<property name="current_session_context_class">thread</property>
<!--创建SessionFactory对象时自动创建数据库表,可取create、create-drop、update等值-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--配置映射文件-->
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
运行结果:
数据库中:
补充说明:需要先建立数据库hibernate,此项目为maven项目。也可以建立普通java项目或web项目。
一个继承层次一张表的优缺点如下:
1、最简单,执行效率最高(无需关联)
2、存在冗余字段,需要加入区分各类的字段;不允许子类属性为not null约束(允许为空)。