JPA ENTITY EXTEND

1. Overview

Relational databases don’t have a straightforward way to map class hierarchies onto database tables.

To address this, the JPA specification provides several strategies:

  • MappedSuperclass – the parent classes, can’t be entities
  • Single Table – The entities from different classes with a common ancestor are placed in a single table.
  • Joined Table – Each class has its table, and querying a subclass entity requires joining the tables.
  • Table per Class – All the properties of a class are in its table, so no join is required.

Each strategy results in a different database structure.

Entity inheritance means that we can use polymorphic queries for retrieving all the subclass entities when querying for a superclass.

Since Hibernate is a JPA implementation, it contains all of the above as well as a few Hibernate-specific features related to inheritance.

2. MappedSuperclass

Using the MappedSuperclass strategy, inheritance is only evident in the class but not the entity model.

Let’s start by creating a Person class that will represent a parent class:

@MappedSuperclass
public class Person {@Idprivate long personId;private String name;// constructor, getters, setters
}

Notice that this class no longer has an @Entity annotation, as it won’t be persisted in the database by itself.

Next, let’s add an Employee subclass:

@Entity
public class MyEmployee extends Person {private String company;// constructor, getters, setters 
}

In the database, this will correspond to one MyEmployee table with three columns for the declared and inherited fields of the subclass.

If we’re using this strategy, ancestors cannot contain associations with other entities.

3. Single Table

The Single Table strategy creates one table for each class hierarchy. JPA also chooses this strategy by default if we don’t specify one explicitly.

We can define the strategy we want to use by adding the @Inheritance annotation to the superclass:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class MyProduct {@Idprivate long productId;private String name;// constructor, getters, setters
}

The identifier of the entities is also defined in the superclass.

Then we can add the subclass entities:

@Entity
public class Book extends MyProduct {private String author;
}
@Entity
public class Pen extends MyProduct {private String color;
}

3.1. Discriminator Values

Since the records for all entities will be in the same table, Hibernate needs a way to differentiate between them.

By default, this is done through a discriminator column called DTYPE that has the name of the entity as a value.

To customize the discriminator column, we can use the @DiscriminatorColumn annotation:

@Entity(name="products")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="product_type", discriminatorType = DiscriminatorType.INTEGER)
public class MyProduct {// ...
}

Here we’ve chosen to differentiate MyProduct subclass entities by an integer column called product_type.

Next, we need to tell Hibernate what value each subclass record will have for the product_type column:

@Entity
@DiscriminatorValue("1")
public class Book extends MyProduct {// ...
}
@Entity
@DiscriminatorValue("2")
public class Pen extends MyProduct {// ...
}

Hibernate adds two other predefined values that the annotation can take — null and not null:

  • @DiscriminatorValue(“null”) means that any row without a discriminator value will be mapped to the entity class with this annotation; this can be applied to the root class of the hierarchy.
  • @DiscriminatorValue(“not null”) – Any row with a discriminator value not matching any of the ones associated with entity definitions will be mapped to the class with this annotation.

Instead of a column, we can also use the Hibernate-specific @DiscriminatorFormula annotation to determine the differentiating values:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("case when author is not null then 1 else 2 end")
public class MyProduct { ... }

This strategy has the advantage of polymorphic query performance since only one table needs to be accessed when querying parent entities.

On the other hand, this also means that we can no longer use NOT NULL constraints on subclass entity properties.

4. Joined Table

Using this strategy, each class in the hierarchy is mapped to its table. The only column that repeatedly appears in all the tables is the identifier, which will be used for joining them when needed.

Let’s create a superclass that uses this strategy:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Animal {@Idprivate long animalId;private String species;// constructor, getters, setters 
}

Then we can simply define a subclass:

@Entity
public class Pet extends Animal {private String name;// constructor, getters, setters
}

Both tables will have an animalId identifier column.

The primary key of the Pet entity also has a foreign key constraint to the primary key of its parent entity.

To customize this column, we can add the @PrimaryKeyJoinColumn annotation:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/11456.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

webpack处理js和css模块化导入导出示例:

webpack默认并不能处理js模块化的导入和导出,依赖于ts-loader和babel-loader webpack.config,js module.exports {entry: ./src/index.ts,output: {filename: main.js,},mode: development, // 或者 productionmodule: {rules: [{test: /\.ts/,exclude: /(node_modules)/,use:…

二维平移矩阵 (2D translate matrix)

2D translate matrix 推荐阅读正文推荐阅读 矢量旋转矩阵 正文 之前我们介绍了矢量旋转矩阵的形式,这里我们来介绍一下平移矩阵的形式。比如,我们我们有一个点,其坐标为 (0,1)。那么我们如何操作才能够将这个点沿着 x 轴正方向平移 1 个单位长度呢? 这里我们以向右移动…

vj题单 P4552 [Poetize6] IncDec Sequence

思路: 一次操作:选一个区间[l, r],把这个区间的数都加1或者都减1,可以将求该数列的差分数组b然后来进行该操作 一次操作的两种种情况:(l可以等于r) 1.b[l]1 b[r1]-1 2.b[l]-1 b[r1]1 Q1:…

PHP 提取数组中的特定的值

需求: 前端展示: (1)之前的页面: (2)修改后的页面: 之前接口返回的数据 : 解决办法:提取tags 中的 ’约 的数组 添加到一个新的数组中去 1:一开…

【CPP】多线程并发—— Mutex 和 Lock

#include <iostream> #include <thread> #include <mutex> #include "my_utils.h"std::mutex mtx; // 全局互斥锁 int shared_data 0; // 共享数据 void increment() { for (int i 0; i < 10; i) { std::cout <<"incre…

2024年去除视频水印的5种方法

如果你从事电影剪辑或者视频编辑工作&#xff0c;你经常需要从优酷、抖音、TikTok下载各种视频片段……。 通常这些视频带有水印和字幕。一些免费软件如CapCut、canva、Filmora也会给你制作的视频打上水印&#xff0c;这些水印嵌入在视频内部。 2024年去除视频水印的5种方法 …

Mysql-用户变量的声明与使用

#声明变量 #1.标识符不能以数字开头 #2.只能使用_或$符号&#xff0c;不能使用其他符号 #3.不能使用系统关键字 setuserName刘德华; select userName:刘青云;#将赋值与查询结合 #查询变量、使用变量&#xff0c;匿名的时候建议加上as select userName as 读取到的userName变量…

Golang面向对象编程(二)

文章目录 封装基本介绍封装的实现工厂函数 继承基本介绍继承的实现字段和方法访问细节多继承 封装 基本介绍 基本介绍 封装&#xff08;Encapsulation&#xff09;是面向对象编程&#xff08;OOP&#xff09;中的一种重要概念&#xff0c;封装通过将数据和相关的方法组合在一起…

java JOptionPane 介绍

JOptionPane是Java Swing库中的一个类,用于创建对话框(Dialogs),以便与用户进行交互。它提供了一种简单的方式来显示消息、警告、错误、输入框等。 主要方法: showMessageDialog(Component parentComponent, Object message):显示一个包含消息的对话框。showInputDialog…

2024OD机试卷-手机App防沉迷系统 (java\python\c++)

题目:手机App防沉迷系统 题目描述 智能手机方便了我们生活的同时,也侵占了我们不少的时间。 “手机App防沉迷系统”能够让我们每天合理地规划手机App使用时间,在正确的时间做正确的事。 它的大概原理是这样的: 在一天24小时内,可以注册每个App的允许使用时段一个时间段只…

Java转Kotlin调用JNI方法异常

一、背景 Java调用JNI方法时没有任何问题&#xff0c;但是使用Java转Kotlin以后出现了崩溃异常&#xff1a;A java_vm_ext.cc:597] JNI DETECTED ERROR IN APPLICATION: jclass has wrong type: 校验参数后没有任何变化&#xff0c;经过分析验证找到解决方案 二、原因…

若依生成树表和下拉框选择树表结构(在其他页面使用该下拉框输入)

1.数据库表设计 生成树结构的主要列是id列和parent_id列&#xff0c;后者指向他的父级 2.来到前端代码生成器页面 导入你刚刚写出该格式的数据库表 3.点击编辑&#xff0c;来到字段 祖籍列表是为了好找到直接父类&#xff0c;不属于代码生成器方法&#xff0c;需要后台编…

【XSRP软件无线电】基于软件无线电平台的QPSK频带通信系统设计

目录&#xff1a; 目录&#xff1a; 一、绪论 1.1 设计背景 1.2 设计目的 二、系统总体方案 2.1 专题调研题目 2.2 调研背景 2.3 设计任务解读 2.4 设计原理 2.4.1 原理框图 2.4.2 功能验证 三、软件设计 3.1 程序解读 3.2 程序设计 3.3 仿真结果&#xff1a; 四、程序代码分析…

网络基础-SSH协议(思科、华为、华三)

SSH&#xff08;Secure Shell&#xff09;是一种用于安全远程访问和安全文件传输的协议。它提供了加密的通信通道&#xff0c;使得用户可以在不安全的网络上安全地远程登录到远程主机&#xff0c;并在远程主机上执行命令、访问文件以及传输文件&#xff0c;本篇主要讲解命令执行…

SpringAI集成本地AI大模型ollama(调用篇)非常简单!!

一&#xff0c;前提准备本地ai模型 1&#xff0c;首先需要去ollama官网下载开源ai到本地 网址&#xff1a;Ollama 直接下载到本地&#xff0c;然后启动ollama 启动完成后&#xff0c;我们可以在cmd中执行ollama可以看到相关命令行 2&#xff0c; 下载ai moudle 然后我们需要…

基于C#开发web网页模板流程-登录界面

前言&#xff0c;首先介绍一下本项目将要实现的功能 &#xff08;一&#xff09;登录界面 实现一个不算特别美观的登录窗口&#xff0c;当然这一步跟开发者本身的设计美学相关&#xff0c;像蒟蒻博主就没啥艺术细胞&#xff0c;勉强能用能看就行…… &#xff08;二&#xff09…

【vector】迭代器

Vector的基本数据结构 可以看到end指向的是数组的最后一个元素&#xff1b; 那么在使用函数遍历的时候就要注意这种清理&#xff1b; 比如计算一个数组前5个数字的最小值&#xff1b; vector<int> prices{2,1,4,2,0,52,12};auto iter_min min_element(prices.begin(),pr…

NSSCTF | [LitCTF 2023]我Flag呢?

这道题没啥好说的&#xff0c;题目标签为源码泄露&#xff0c;我们直接CtrlU查看网页源码就能在最后找到flag 本题完

深入学习指针2

前言 hello,我又来了&#xff0c;今天有我继续带领大家深入的学习指针&#xff0c;通过上次的学习&#xff0c;我们已经了解到了指针的基本概念&#xff0c;指针如何使用&#xff0c;指针使用的益处&#xff0c;以及一些相关的概念&#xff0c;那今天我们就继续深入的学习&am…

Vue3专栏项目 -- 二、自定义From组件(下)

需求分析&#xff1a; 现在我们还需要一个整体的表单在单击某个按钮的时候可以循环的验证每个input的值&#xff0c;最后我们还需要有一个事件可以得到最后验证的结果&#xff0c;从而进行下一步的操作 如下&#xff0c;我们应该有一个form表单包裹着全部的input表单&#xf…