spring-data-jpa实体类继承抽象类如何映射父类的属性到数据库
2023-09-27 14:24:46 时间
在抽象父类上加上注解@MappedSuperclass
@MappedSuperclass
public class Pet {
private Integer id;//id
private String name;//名字
private String variety;//品种
private double weight;//体重
private Integer age;//年龄
private char vaccine;//是否注射疫苗 y:已注射 n:未注射 d:未知
private Integer ownerId;//主人id
private char LookingForOwner;//是否在寻找主人 y:正在寻找 n:未在寻找
private String pic;//照片地址
@Id
@GeneratedValue
public String getPic() {
return pic;
}
/剩下的get set方法省略
子类直接继承抽象父类并加上@Entity注解
并用@Table(name = "t_dog")指明映射的表
@Entity
@Table(name = "t_dog")
public class Dog extends Pet{
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Dog</span><span class="hljs-params">()</span> </span>{
}
}
此时运行项目生成的表中有父类中的属性,并且id作为自增主键
原文地址:https://www.jianshu.com/p/be5b9e4fa91e </div>
相关文章
- 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
- Spring Boot 整合 Flowable-ui-modeler 6.7.2
- 【Spring源码分析】Bean加载流程概览
- 《Spring 3.0就这么简单》——1.5 业务层
- spring属性注入
- Spring StringUtils类中几个有用的字符串处理方法
- 在Spring Boot中使用内存数据库
- spring框架漏洞整理(Spring Boot Actuator相关漏洞)
- 【Redis】基于Spring Cache + Redis + Jackson的注解式自动缓存方案保姆式教程(2022最新)
- Spring源码阅读---Filter中使用@Autowired失效?
- 【spring框架】bean属性注入方式
- spring 通过编程来获取属性文件
- 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)
- 曹工说Spring Boot源码(27)-- Spring的component-scan,光是include-filter属性的各种配置方式,就够玩半天了
- Spring MVC 框架搭建及详解