SpringBoot JPA not-null property references a null or transient value 问题解决

. ____ _ __ _ _

/\\ / _' __ _ () __ __ _ \ \ \ \

( ( )\___ | '_ | '| | ' \/ _` | \ \ \ \

\\/ _)| |)| | | | | || (| | ) ) ) )

' || .__|| ||| |\__, | / / / /

=========||==============|/=///_/

:: Spring Boot :: (v2.7.13)

问题描述:

使用JPA编辑实体时报错:

not-null property references a null or transient value : com.psquare.book.entity.system.Role.createAt; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.psquare.book.entity.system.Role.createAt

解决方法:

报错的字段恰好时更新时不需要的字段,在字段对应的@Column注解中设置updatable=false解决

复制代码
@Column(nullable = false, updatable = false)
private Date createAt;
@Column(nullable = false, length = 50, updatable = false)
private String createBy;

也可以先使用ID获取当前需要更新的实体,然后修改需要更新的字段后再使用save保存,这样每次更新都是全字段,如果字段比较多对数据库性能会有影响。可以在实体类上使用@DynamicUpdate注解,这样hibernate在生成更新SQL时就只包含值有变更并且不为null的字段。 但这样如果在当前事务中没有获取过当前实体,Hibernate会先查询下数据库,以便获取到实体进行字段对比。

未使用@DynamicUpdate注解生成的更新SQL edit:test_hot3

Hibernate: select role0_.id as id1_1_0_, role0_.create_at as create_a2_1_0_, role0_.create_by as create_b3_1_0_, role0_.update_at as update_a4_1_0_, role0_.update_by as update_b5_1_0_, role0_.description as descript6_1_0_, role0_.name as name7_1_0_ from role role0_ where role0_.id=?

Hibernate: update role set update_at=?, update_by=?, description=?, name=? where id=?

添加@DynamicUpdate

复制代码
@DynamicUpdate
public class Role

使用了@DynamicUpdate注解后生成的更新SQL edit:test_hot4

Hibernate: select role0_.id as id1_1_0_, role0_.create_at as create_a2_1_0_, role0_.create_by as create_b3_1_0_, role0_.update_at as update_a4_1_0_, role0_.update_by as update_b5_1_0_, role0_.description as descript6_1_0_, role0_.name as name7_1_0_ from role role0_ where role0_.id=?

Hibernate: update role set update_at=?, name=? where id=?
使用了@DynamicUpdate注解后,在service中先load再更新 生成的SQL edit:test_hot5

Hibernate: update role set update_at=?, name=? where id=?

相关推荐
麦麦鸡腿堡17 分钟前
Java_类的加载
java·开发语言
JIngJaneIL26 分钟前
基于java + vue校园快递物流管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js
超级大只老咪31 分钟前
数组的正向存储VS反向存储(Java)
java·开发语言·python
毕设源码-赖学姐36 分钟前
【开题答辩全过程】以 基于JSP的物流信息网的设计与实现为例,包含答辩的问题和答案
java·开发语言
Honmaple36 分钟前
Spring AI 2.x 发布:全面拥抱 Java 21,Redis 史诗级增强
java·人工智能·spring
canonical_entropy1 小时前
Nop入门:增加DSL模型解析器
spring boot·后端·架构
代码or搬砖1 小时前
Java集合-Set讲解
java·开发语言
渣娃-小晴晴1 小时前
java集合在并发环境下应用时的注意事项
java·后端
北极糊的狐1 小时前
若依系统报错net::ERR_CONNECTION_TIMED_OUT的原因
java·windows·sql·mybatis