SpringDataJPA笔记(15)--Inheritance注解详解之JOINED

2023-10-11 23:50

本文主要是介绍SpringDataJPA笔记(15)--Inheritance注解详解之JOINED,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SpringDataJPA笔记(15)–Inheritance注解详解之JOINED

在JPA中使用映射注解Inheritance,有一种策略是JOINED

JOINED – 每个类分别生成一张单独的表,但是每张表只有自己的属性,没有父类的属性,通过外键关联的形式使两张表关联起来

还是简单写个demo测试一下

先写三个实体类

@Data
@Table(name = "inheritance_joined_tb")
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class InheritanceJoinedEntity implements Serializable {private static final long serialVersionUID = 8139674112977338603L;@Idprivate Long id;@Column(name = "joined_name")private String joinedName;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_joined_one_tb")
public class InheritanceJoinedOneEntity extends InheritanceJoinedEntity {private static final long serialVersionUID = 9042160117012026124L;@Column(name = "joined_one")private String joinedOne;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_joined_two_tb")
public class InheritanceJoinedTwoEntity extends InheritanceJoinedEntity {private static final long serialVersionUID = 9042160117012026124L;@Column(name = "joined_two")private String joinedTwo;
}

然后是三个repository接口类

public interface InheritanceJoinedRepository extends JpaRepository<InheritanceJoinedEntity, Long>, JpaSpecificationExecutor<InheritanceJoinedEntity>, Serializable {
}
public interface InheritanceJoinedOneRepository extends JpaRepository<InheritanceJoinedOneEntity, Long>, JpaSpecificationExecutor<InheritanceJoinedOneEntity>, Serializable {
}
public interface InheritanceJoinedTwoRepository extends JpaRepository<InheritanceJoinedTwoEntity, Long>, JpaSpecificationExecutor<InheritanceJoinedTwoEntity>, Serializable {
}

然后写controller类用于测试

@Autowiredprivate InheritanceJoinedRepository inheritanceJoinedRepository;@Autowiredprivate InheritanceJoinedOneRepository inheritanceJoinedOneRepository;@Autowiredprivate InheritanceJoinedTwoRepository inheritanceJoinedTwoRepository;@PostMapping("/single")public InheritanceJoinedEntity single(@RequestBody InheritanceJoinedEntity inheritanceJoinedEntity) {return inheritanceJoinedRepository.save(inheritanceJoinedEntity);}@PostMapping("/joined/one")public InheritanceJoinedOneEntity joined(@RequestBody InheritanceJoinedOneEntity inheritanceJoinedEntity) {return inheritanceJoinedOneRepository.save(inheritanceJoinedEntity);}@PostMapping("/joined/two")public InheritanceJoinedTwoEntity joined(@RequestBody InheritanceJoinedTwoEntity inheritanceJoinedEntity) {return inheritanceJoinedTwoRepository.save(inheritanceJoinedEntity);}@GetMapping("/joined")public List<InheritanceJoinedEntity> joined() {return inheritanceJoinedRepository.findAll();}@GetMapping("/joined/one")public List<InheritanceJoinedOneEntity> joinedOne() {return inheritanceJoinedOneRepository.findAll();}@GetMapping("/joined/two")public List<InheritanceJoinedTwoEntity> joinedTwo() {return inheritanceJoinedTwoRepository.findAll();}

启动程序,查看数据库,有三张表
在这里插入图片描述

分别通过三个接口写入数据,再查看数据库数据
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

通过接口查询数据的结果

在这里插入图片描述
在这里插入图片描述

欢迎关注微信交流
在这里插入图片描述

这篇关于SpringDataJPA笔记(15)--Inheritance注解详解之JOINED的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/191737

相关文章

linux本机进程间通信之UDS详解

《linux本机进程间通信之UDS详解》文章介绍了Unix域套接字(UDS)的使用方法,这是一种在同一台主机上不同进程间通信的方式,UDS支持三种套接字类型:SOCK_STREAM、SOCK_DGRA... 目录基础概念本机进程间通信socket实现AF_INET数据收发示意图AF_Unix数据收发流程图A

Go 1.23中Timer无buffer的实现方式详解

《Go1.23中Timer无buffer的实现方式详解》在Go1.23中,Timer的实现通常是通过time包提供的time.Timer类型来实现的,本文主要介绍了Go1.23中Timer无buff... 目录Timer 的基本实现无缓冲区的实现自定义无缓冲 Timer 实现更复杂的 Timer 实现总结在

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

Apache伪静态(Rewrite).htaccess文件详解与配置技巧

《Apache伪静态(Rewrite).htaccess文件详解与配置技巧》Apache伪静态(Rewrite).htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令,主要的... 一、.htAccess的基本作用.htaccess是一个纯文本文件,它里面存放着Apache服务器

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

golang panic 函数用法示例详解

《golangpanic函数用法示例详解》在Go语言中,panic用于触发不可恢复的错误,终止函数执行并逐层向上触发defer,最终若未被recover捕获,程序会崩溃,recover用于在def... 目录1. panic 的作用2. 基本用法3. recover 的使用规则4. 错误处理建议5. 常见错

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu