SpringBoot + JPA问题汇总
2020-12-13 01:39
标签:bin poi 存在 添加 tps ons conf exception red 1 Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.xxx.ProjectDTO SpringBoot + JPA问题汇总 标签:bin poi 存在 添加 tps ons conf exception red 原文地址:https://www.cnblogs.com/klyjb/p/11003475.html实体类有继承父类,但父类没有单独标明注解
异常表现
解决方式
@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
解决方式
@NoRepositoryBean
告知SpringBoot该类不是一个存储库
@NoRepositoryBean
public interface BaseJpaRepository
实体类创建后没有单独标明注解@Entity
异常表现
***************************
APPLICATION FAILED TO START
***************************
...
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
...
解决方式
@Entity
注解
@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)]
解决方式
@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;
}
解决方式
@GeneratedValue
直接指定生成规则,就会抛出上述异常
GeneratedType.AUTO
auto_increment
,这对应的应该是GenerationType.IDENTITY
public class BaseDTO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = 0L;
}
修改JPA的字段命名规则为驼峰式
异常表现
Caused by: java.sql.SQLSyntaxErrorException: Unknown column ‘create_time‘ in ‘field list‘
@Column
private Date createTime = DateUtil.getSysDate();
解决方式
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
获取单个实体类JSON转换异常
异常表现
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)
解决方式
上一篇:开源Web框架PHP与Ruby on Rail的比较
下一篇:CSS 选择器的学习