Hibernate framework-03-01-Hibernate inheritance relationship mapping

Project address

https://yxmiaoyu.lanzous.com/b01c67olc Password: 74k5
https://yxmiaoyu.lanzous.com/b01c67olc
密码:74k5

Insert picture description here

具体类就是实体化类
类层次就是继承层次






Table per concrete class Each concrete class corresponds to a table

Insert picture description here

Polymorphic query, one sentence, you can look up two different kinds of information.

Insert picture description here
Insert picture description here
Insert picture description here

Each class has a table, and there is no relationship between the table and the table, and it is operated separately.

hbm mapping file configuration.

The details are in the HibernateCH03-01 project
Insert picture description here

Annotation mode configuration

The details are in the HibernateCH03-02 project
Insert picture description here

The base class object does not need to be persisted, just add the @MappedSuperClass attribute to the base class
Insert picture description here
Insert picture description here





Table per class hierarchy Each class level corresponds to a table

  • The relational data model supports inheritance relations and polymorphism.
    One statement, multiple queries
  • Add additional fields to the table for the type of molecular class. The table contains the fields corresponding to the attributes of the parent class and all subclasses.
    Judge by extra fields
  • Support polymorphic query, that is, when retrieving the parent class object from the database, all the subclass objects are included at the same time.

Insert picture description here

Insert picture description here

hbm mapping file configuration

The details are in the HibernateCH03-03 project
Insert picture description here
Insert picture description here

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hibernate.entity">
		<!-- class映射的是基类和数据库表的命名 -->
		<class name="Employee" table="TABLE01">
		<!-- 映射ID属性 -->
			<id name="id">
				<generator class="identity"/>
			</id>
			<!--  额外字段,区分记录对象 -->
			<discriminator column="linshi1"></discriminator>
			<!-- 基类中定义的属性 -->
			<property name="name"></property>
			<!-- 映射每一个子类,name=子类类名,discriminator-value=额外字段取值 -->
			<subclass name="HourlyEmployee" discriminator-value="HE">
				<!-- 子类中定义的属性,因为是同名 所以colimn省略了 -->			
        		<property name="rate"/>		
			</subclass>
			<subclass name="SalariedEmployee" discriminator-value="SE">			
        		<property name="salary"/>		
			</subclass>
		</class>

</hibernate-mapping>


Insert picture description here

package com.hibernate.ui;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hibernate.entity.Employee;
import com.hibernate.entity.HourlyEmployee;
import com.hibernate.entity.SalariedEmployee;
import com.hibernate.util.HibernateUtil;

public class Test {
    
    

	public static void main(String[] args) {
    
    
//		HourlyEmployee h =new HourlyEmployee();
//		h.setName("h1");
//		h.setRate(24.0);
//		SaveEmployee(h);
//		System.out.println(h.toString());
		
		
//		SalariedEmployee s = new SalariedEmployee();
//		s.setName("s1");
//		s.setSalary(30.0);
//		SaveEmployee(s);
//		System.out.println(s.toString());
		
		
		getEmployee();
	}

	
	private static void SaveEmployee(Employee e) {
    
    
		Session session=HibernateUtil.openSession();
		Transaction tx=session.beginTransaction();
		session.save(e);
		tx.commit();
		session.close();
	}
	private static void getEmployee() {
    
    
		Session session=HibernateUtil.openSession();
		HourlyEmployee he =session.get(HourlyEmployee.class, new Integer(2));
		System.out.println(he);
		SalariedEmployee sa =session.get(SalariedEmployee.class,new Integer(1));
		System.out.println(sa);
	}
}

Insert picture description here

Annotation mode configuration

The details are in the HibernateCH03-04 project

Insert picture description here

![package com.hibernate.entity;

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;



@Entity 
//表示当前类型需要持久化
//继承关系的生成策略
//单一的表
@Table(name="table01")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
//额外字段
@DiscriminatorColumn(name="LINSHI1")

public class Employee {
    
    
	private Integer id;
	private String name;
	
	
@Id
@GeneratedValue(generator = "linshi2")
@GenericGenerator(strategy = "increment",name="linshi2")
	public Integer getId() {
    
    
		return id;
	}
	public void setId(Integer id) {
    
    
		this.id = id;
	}

	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	@Override
	public String toString() {
    
    
		return "Employee [id=" + id + ", name=" + name + ", getId()=" + getId() + ", getName()=" + getName()
				+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
				+ "]";
	}
	
	
	

}
](https://img-blog.csdnimg.cn/20210307210915583.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjI3NjA4,size_16,color_FFFFFF,t_70)

Insert picture description here

Insert picture description here

Insert picture description here





Table per class Each class corresponds to a table

Each class corresponds to a table, and polymorphic queries are also supported.

  • In the relational data model, the foreign key reference relationship is used to represent the inheritance relationship, and the table corresponding to the subclass has the primary key of the foreign key referring to the corresponding table of the parent class.
  • Each class and interface in the inheritance relationship corresponds to a table.
  • Support polymorphic query.
    Insert picture description here
    The most complex, many and many foreign keys.

pk=primary key
fk=foreign key

Insert picture description here

hbm mapping file configuration

The details are in the HibernateCH03-05 project

Insert picture description here
Insert picture description here

Insert picture description here

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hibernate.entity">
		<!-- class映射的是基类和数据库表的命名 -->
		<class name="Employee" table="hibernate_03_05_employee">
		<!-- 映射ID属性 -->
			<id name="id">
				<generator class="identity"/>
			</id>



			<!-- 基类中定义的属性 -->
			<property name="name"></property>
			<!-- 映射每一个子类,name=子类类名,table=数据库名-->
			<joined-subclass name="HourlyEmployee" table="hibernate_03_05_hourly_employee">
				<!-- 子类中定义的属性,因为是同名 所以colimn省略了 -->		
				<!-- key影射的是子类映射的主键 -->
				<key column="employee_id"/>	
        		<property name="rate"/>		
			</joined-subclass>
			<joined-subclass name="SalariedEmployee" table="hibernate_03_05_salaried_employee">		
				<key column="employee_id"/>	
        		<property name="salary"/>		
			</joined-subclass>
		</class>

</hibernate-mapping>



Insert picture description here

Insert picture description here

Every time an object is saved, two SQL insert statements need to be executed.
When querying, they are all linked queries.

Annotation file configuration

The details are in the HibernateCH03-06 project

Insert picture description here

package com.hibernate.entity;

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;



@Entity 
//表示当前类型需要持久化
//继承关系的生成策略
//单一的表
@Table(name="hibernate_03_05_employee")
@Inheritance(strategy=InheritanceType.JOINED)

public class Employee {
    
    
	private Integer id;
	private String name;
	
	
@Id
@GeneratedValue(generator = "linshi2")
@GenericGenerator(strategy = "increment",name="linshi2")
	public Integer getId() {
    
    
		return id;
	}
	public void setId(Integer id) {
    
    
		this.id = id;
	}

	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	@Override
	public String toString() {
    
    
		return "Employee [id=" + id + ", name=" + name + ", getId()=" + getId() + ", getName()=" + getName()
				+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
				+ "]";
	}
	
	
	

}


Insert picture description here

Insert picture description here

Insert picture description here

Comparison of three mapping methods

Insert picture description here

Insert picture description here

The second way has the best query performance.

Insert picture description here
The first method has poor maintainability in the later stage

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/114300476