SpringBoot + JPA问题汇总
实体类有继承父类,但父类没有单独标明注解
异常表现
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.xxx.ProjectDTO
解决方式
- 可以看到ProjectDTO有继承一个BaseDTO,那么在父类中肯定存在某些字段需要与数据库表字段对应
- 因此类父需要使用
@MappedSuperclass
标注为映射的父类,即可解决上述问题@Entity
@Table(name = "al_project")
public class ProjectDTO extends BaseDTO { ... } @MappedSuperclass
public class BaseDTO { ... }
自定义接口实现了JpaRepository,但没有单独标明注解
异常表现
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseJpaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
...
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
解决方式
- 上述报错中提到BaseJpaRepository创建失败,其实是因为该接口继承JpaRepository,继承CrudRepository同理
- 导致SpringBoot把该类认为是Jpa的某一个存储库,所以需要添加
@NoRepositoryBean
告知SpringBoot该类不是一个存储库
@NoRepositoryBean
public interface BaseJpaRepository<T, ID> extends JpaRepository<T, ID> { ... }
实体类创建后没有单独标明注解@Entity
异常表现
- 需要注意的是这个错误可能不完全以下内容
***************************
APPLICATION FAILED TO START
***************************
...
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
...
解决方式
- 检查所有的实体类,并添加全部上
@Entity
注解 - 需要注意的是以下实体类ConfigDTO继承了BaseDTO,而BaseDTO作为父类不应该使用
@Entity
注解 - 而使用应该问题上述提到中的
@MappedSuperclass
标明的英文映射父类
@Entity
@Table(name = "al_config")
public class ConfigDTO extends BaseDTO { ... }
实体类中的某个字段是一个对象,却错误的使用了@Column注解
异常表现
Caused by: org.hibernate.MappingException: Could not determine type for: com.asing1elife.teamnote.dto.ProjectGroupDTO, at table: al_project, for columns: [org.hibernate.mapping.Column(project_group)]
解决方式
- 以下类中ProjectGroupDTO应该是一个对象,所以根据应该具体映射情况决定的英文使用
@ManyToOne
还是@OneToMay
,或其他对象映射规则
@Entity
@Table(name = "al_project")
public class ProjectDTO extends BaseDTO {
@Column
private ProjectGroupDTO projectGroup;
}
实体类ID使用了错误的生成规则
异常表现
java.sql.SQLSyntaxErrorException: Table 'asl_station.hibernate_sequence' doesn't exist
public class BaseDTO {
@Id
@GeneratedValue
private Long id = 0L;
}
解决方式
- 上述类中在ID上通过
@GeneratedValue
直接指定生成规则,就会抛出上述异常 - 因为默认的生成规则是
GeneratedType.AUTO
- 而我们在创建表时,通常使用的ID自增规则都是
auto_increment
,这对应的应该是GenerationType.IDENTITY
- 所以应该使用如下方式指定ID的自增规则
public class BaseDTO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = 0L;
}
修改JPA的字段命名规则为驼峰式
异常表现
- 在实体类和数据库中对应的字段都是createTime,但是JPA在连接对应表时却抛出如下错误
- 因为JPA默认的字段映射规则是下划线风格
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'create_time' in 'field list'
@Column
private Date createTime = DateUtil.getSysDate();
解决方式
- 在配置文件中指定JPA的字段映射规则为驼峰式即可
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
获取单个实体类JSON转换异常
异常表现
- Hibernate的获取单个实体类数据后,为会每个实体类添加一个
hibernateLazyInitializer
属性,改属性在进行JSON转换时抛出异常
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.asing1elife.teamnote.core.bean.ResponseData["data"]->com.asing1elife.teamnote.dto.OrganizationDTO$HibernateProxy$f3Tr4vBI["hibernateLazyInitializer"])
解决方式一
- 在实体类顶部添加
@JsonIgnoreProperties("hibernateLazyInitializer")
,确保数据转换时会过滤掉休眠生成的属性@JsonIgnoreProperties("hibernateLazyInitializer")
public class BaseDTO { ... }
解决方式二
- 上述根据报错
disable SerializationFeature.FAIL_ON_EMPTY_BEANS
可以尝试将JSON转换时的以下属性设置为false
spring:
jackson:
serialization:
fail-on-empty-beans: false
前端数据传递到后端过程中,反序列失败
异常表现
- 杰克逊反序列化时需要无参构造函数,因为数据对应的实体类某个对象属性没有无参构造函数,就会抛出以下异常
(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
解决方式
- 检查实体类自身以及所有对象属性,为每个对象添加对应的无参构造函数即可解决
- 自参考博客园
SpringBoot + JPA问题汇总的更多相关文章
- 补习系列(19)-springboot JPA + PostGreSQL
目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限
开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...
- 带着新人学springboot的应用08(springboot+jpa的整合)
这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...
- springboot+jpa+mysql+redis+swagger整合步骤
springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...
- springboot+jpa+mysql+swagger整合
Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency> < ...
- SpringBoot JPA + H2增删改查示例
下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...
- SpringBoot Jpa入门案例
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 我们先来了解一下是什么是springboot jpa,springboo ...
- SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy
问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...
随机推荐
- Thread.Sleep太久,界面卡死
在Winform程序的UI界面使用Thread.Sleep,窗体界面会被卡死,如图1所示,程序sleep 5000毫秒,时间到了之后,按钮的名称才更改过来,窗体也能被鼠标拖动.而用Delay方法,就能 ...
- mysql中left join right join inner join用法分析
mysql数据库中的关联查询,基本都会用到left join,right join,inner join等查询方式,今天来说说这三种用法的区别 1.创建表test1,test2,插入测试数据 #创建表 ...
- jQuery插件之路(三)——文件上传(支持拖拽上传)
好了,这次咱一改往日的作风,就不多说废话了,哈哈.先贴上源代码地址,点击获取.然后直接进入主题啦,当然,如果你觉得我有哪里写的不对或者欠妥的地方,欢迎留言指出.在附上一些代码之前,我们还是先来了解下, ...
- [Spring cloud 一步步实现广告系统] 15. 使用开源组件监听Binlog 实现增量索引准备
MySQL Binlog简介 什么是binlog? 一个二进制日志,用来记录对数据发生或潜在发生更改的SQL语句,并以而进行的形式保存在磁盘中. binlog 的作用? 最主要有3个用途: 数据复制( ...
- what is the CCA?
Clear Channel Assessment (CCA) is one of two carrier sense mechanisms in WLAN (or WiFi). It is defin ...
- 面试java后端面经_3
小姐姐说:你一点都不懂表达,一点都不懂爱情,一点也不爱我! 你答:你知道吗,我听说过一个这样的故事,讲的就是有一个小女孩和一个男孩在一起,小男孩呢很不幸是位聋哑人,虽然如此,但是他们的日子过得特别的美 ...
- hadoop2.7+spark2.2+zookeeper3.4.简单安装
1.zookeeper的安装##配置/etc/hosts192.168.88.130 lgh192.168.88.131 lgh1192.168.88.132 lgh2 ##安装java8 解压配置环 ...
- c#自定义控件中的事件处理
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- 使用pandoc简单教程
使用pandoc作为过滤器 {#step-4-using-pandoc-as-a-filter} 类型 pandoc 并按Enter键.你应该看到光标就在那里,等着你输入一些东西.输入: Hello ...
- c# oracle 数据库连接以及参数化查询
private string OracleSearchDemo(string cadqueueId) { string address = null; using (OracleConnection ...