解决JPA “Null value was assigned to a property of primitive type setter of” 异常

org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property of primitive type setter of com.huangx.model.entity.User.userId; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.huangx.model.entity.User.userId 错误信息

今天在使用JPA保存数据时抛出了如下错误信息:

[ERROR]-[20-01-03 09:12:58 763]-[http-nio-8080-exec-5]-[c.g.e.s.UserService]: 保存用户信息失败

org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property of primitive type setter of com.huangx.model.entity.User.userId; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.huangx.model.entity.User.userId

    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)

    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)

    ...

    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)

    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)

    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

从上面异常 “Null value was assigned to a property of primitive type setter of com.huangx.model.entity.User.userId; ” 将 Null值赋值给原始类型的 User.userId 的 setter 失败。User.java 如下:

@Data
@Entity
@Table(name = "users")
public class AlarmThresholdLog {
    @Id
    @Column(name = "user_id")
    private long userId;
}

将上面的

private long userId;

改为

private Long userId;

即可解决该问题。

活着就要学习,学习不是为了活着。 —— 培根
1 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号