解决 nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property

本文介绍了一个关于Spring框架中出现的Null值被分配给属性错误的问题,详细解释了错误发生的原因及解决方案,主要涉及数据库字段类型与实际数据一致性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Null value was assigned to a property

错误如下:

在这里插入图片描述
在这里插入图片描述

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property [class com.liu.pojo.Student.stuAge] of primitive type setter of com.liu.pojo.Student.stuAge; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.liu.pojo.Student.stuAge] of primitive type setter of com.liu.pojo.Student.stuAge] with root cause

java.lang.IllegalArgumentException: Can not set int field com.liu.pojo.Student.stuAge to null value
	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_151]
	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_151]
	at sun.reflect.UnsafeIntegerFieldAccessorImpl.set(UnsafeIntegerFieldAccessorImpl.java:80) ~[na:1.8.0_151]
	at java.lang.reflect.Field.set(Field.java:764) ~[na:1.8.0_151]
	at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:52) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:680) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:144) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:5119) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntityFromEntityEntryLoadedState(TwoPhaseLoad.java:290) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:185) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:153) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1203) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.processResultSet(Loader.java:1004) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.doQuery(Loader.java:962) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:352) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.doList(Loader.java:2857) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.doList(Loader.java:2839) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2671) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.Loader.list(Loader.java:2666) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1412) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.query.Query.getResultList(Query.java:165) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:126) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:154) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at 

造成原因

数据库中表字段对应的数据与该字段的数据类型不一致导致的,比如表字段的数据类型为Int,而数据为NULL,Int类型不能赋值为NULL,就是这样子数据类型不一致才会导致这个错误
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解决问题

  • 数据库给值,或者为空的话默认给个符合条件的值
    在这里插入图片描述
  • 重新刷新,问题解决
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@素素~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值