今天在使用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;
即可解决该问题。