实体类有继承父类,但父类没有单独标明注解

异常表现

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.xxx.ProjectDTO

解决方式

  1. 可以看到ProjectDTO有继承一个BaseDTO,那么在父类中肯定存在某些字段需要与数据库表字段对应
  2. 因此类父需要使用@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

解决方式

  1. 上述报错中提到BaseJpaRepository创建失败,其实是因为该接口继承JpaRepository,继承CrudRepository同理
  2. 导致SpringBoot把该类认为是Jpa的某一个存储库,所以需要添加@NoRepositoryBean告知SpringBoot该类不是一个存储库
@NoRepositoryBean
public interface BaseJpaRepository<T, ID> extends JpaRepository<T, ID> { ... }

实体类创建后没有单独标明注解@Entity

异常表现

  1. 需要注意的是这个错误可能不完全以下内容
***************************
APPLICATION FAILED TO START
***************************
...
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
...

解决方式

  1. 检查所有的实体类,并添加全部上@Entity注解
  2. 需要注意的是以下实体类ConfigDTO继承了BaseDTO,而BaseDTO作为父类不应该使用@Entity注解
  3. 而使用应该问题上述提到中的@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)]

解决方式

  1. 以下类中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;
}

解决方式

  1. 上述类中在ID上通过@GeneratedValue直接指定生成规则,就会抛出上述异常
  2. 因为默认的生成规则是 GeneratedType.AUTO
  3. 而我们在创建表时,通常使用的ID自增规则都是auto_increment,这对应的应该是GenerationType.IDENTITY
  4. 所以应该使用如下方式指定ID的自增规则
public class BaseDTO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = 0L;
}

修改JPA的字段命名规则为驼峰式

异常表现

  1. 在实体类和数据库中对应的字段都是createTime,但是JPA在连接对应表时却抛出如下错误
  2. 因为JPA默认的字段映射规则是下划线风格
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'create_time' in 'field list'
@Column
private Date createTime = DateUtil.getSysDate();

解决方式

  1. 在配置文件中指定JPA的字段映射规则为驼峰式即可
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

获取单个实体类JSON转换异常

异常表现

  1. 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"])

解决方式一

  1. 在实体类顶部添加@JsonIgnoreProperties("hibernateLazyInitializer"),确保数据转换时会过滤掉休眠生成的属性

    @JsonIgnoreProperties("hibernateLazyInitializer")
    public class BaseDTO { ... }

解决方式二

  1. 上述根据报错disable SerializationFeature.FAIL_ON_EMPTY_BEANS可以尝试将JSON转换时的以下属性设置为false
spring:
jackson:
serialization:
fail-on-empty-beans: false

前端数据传递到后端过程中,反序列失败

异常表现

  1. 杰克逊反序列化时需要无参构造函数,因为数据对应的实体类某个对象属性没有无参构造函数,就会抛出以下异常
(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)

解决方式

  1. 检查实体类自身以及所有对象属性,为每个对象添加对应的无参构造函数即可解决
  2. 自参考博客园
 
 
 
 

SpringBoot + JPA问题汇总的更多相关文章

  1. 补习系列(19)-springboot JPA + PostGreSQL

    目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...

  2. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限

    上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...

  3. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

    开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...

  4. 带着新人学springboot的应用08(springboot+jpa的整合)

    这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...

  5. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  6. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  7. SpringBoot JPA + H2增删改查示例

    下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...

  8. SpringBoot Jpa入门案例

    版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 我们先来了解一下是什么是springboot jpa,springboo ...

  9. SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy

    问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...

随机推荐

  1. 03、Swagger2和Springmvc整合详细记录(爬坑记录)

    时间 内容 备注 2018年6月18日 基本使用 spirngmvc整合swagger2 开始之前这个系列博文基本是,在项目的使用中一些模块的内容记录,但是后期逐渐优化,不单单是整合内容. swagg ...

  2. kubeproxy源码分析

    kubernetes离线安装包,仅需三步 kube-proxy源码解析 ipvs相对于iptables模式具备较高的性能与稳定性, 本文讲以此模式的源码解析为主,如果想去了解iptables模式的原理 ...

  3. spring-boot 示例大全

    spring-boot-demo Spring Boot 学习示例,将持续更新... 本项目基于spring boot 最新版本(2.1.7)实现 什么是spring-boot Spring Boot ...

  4. 802.11学习笔记1-WIFI参数含义

    研究下wifi参数的含义 #The word of "Default" must not be removed Default CountryRegion= CountryRegi ...

  5. Spring Boot + Security + JWT 实现Token验证+多Provider——登录系统

    首先呢就是需求: 1.账号.密码进行第一次登录,获得token,之后的每次请求都在请求头里加上这个token就不用带账号.密码或是session了. 2.用户有两种类型,具体表现在数据库中存用户信息时 ...

  6. 100天搞定机器学习|Day21 Beautiful Soup

    前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...

  7. VU TPS QPS RT 计算公式

    1.背景 最近看了阿里巴巴中间件写的一篇文章,讲述了关于并发,RPS,RT之间的关系.感觉收获颇丰.自己使用JMeter工具对公式进行了验证. 2.验证 我们先来看几个基础知识定义: TPS:每秒完成 ...

  8. 【KakaJSON手册】06_Model转JSON

    前面的文章介绍了如何利用KakaJSON进行JSON转Model,从这篇文章开始介绍如何将Model转成JSON 生成JSON和JSONString struct Car: Convertible { ...

  9. 谈谈surging 微服务引擎 2.0的链路跟踪和其它新增功能

    一.前言 surging是基于.NET CORE 服务引擎.初始版本诞生于2017年6月份,经过NCC社区二年的孵化,2.0版本将在2019年08月28日进行发布,经历二年的发展,已经全部攘括了微服务 ...

  10. python模块之junos-eznc

    一.简介 本文将使用python模块中的junos-eznc来控制juniper的 Junos OS系统,此模块可以在windows平台和UNIX平台上使用 二.实验环境 1.操作系统:win10 2 ...