首页 > 解决方案 > 保存后标识符不能为空错误

问题描述

我正在尝试为我们的一个项目构建 POC,并且一直在尝试使用 spring data jpa 模块。我用的是h2数据库,数据库只有一张表。我有一个映射到表的实体对象。我有一个获取 json 对象的测试控制器。我从中创建了一个实体对象,并将实体对象传递给存储库。我有一个使用 save() 方法将新记录插入到表中的服务。我看到生成了正确的 id,但操作失败并出现异常 - “保存标识符后不得为空”。

我在这里尝试了 id 的所有不同组合,但我猜代码所指的标识符可能是我不知道的另一个标识符。BasicPersistentEntity(spring 类)中有一个静态类 AbsentIdentifierAccessor,它被调用来获取标识符。

私有静态类 AbsentIdentifierAccessor 扩展 TargetAwareIdentifierAccessor {

    public AbsentIdentifierAccessor(Object target) {
        super(target);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
     */
    @Override
    @Nullable
    public Object getIdentifier() {
        return null;
    }
}

有没有人在使用存储库方法时遇到过类似的问题?这里的任何见解都会在这里有所帮助。

@Repository
public interface TimeSheetRepository extends JpaRepository<Timesheet,  Long> {

}

@Entity
@Table(name = "Timesheet")
public class Timesheet implements Serializable {

private static final long serialVersionUID = 7157684222538550301L;

@Id
@GeneratedValue
private Long timesheet_entry_id;

@Column(length = 100,nullable = false)
private String client;

@Column(length = 100,nullable = false)
private String project;

public Long getTimesheet_entry_id() {
    return timesheet_entry_id;
}

public void setTimesheet_entry_id(Long timesheet_entry_id) {
    this.timesheet_entry_id = timesheet_entry_id;
}
}

堆栈跟踪:java.lang.IllegalArgumentException:在 org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.9.RELEASE.jar:5.1. 9.RELEASE] 在 org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:109) ~[spring-data-jdbc-1.0.10.RELEASE.jar:1.0.10.RELEASE] 在 org. springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:45) ~[spring-data-jdbc-1.0.10.RELEASE.jar:1.0.10.RELEASE] 在 sun.reflect.NativeMethodAccessorImpl.invoke0 (本机方法)~[na:1.8.0_151] 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_151] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)~[na:1.8。 0_151] 在 java.lang.reflect.Method.invoke(未知来源)~[na:1.8.0_151] 在 org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] 在org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.data .repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.aop.framework。 ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.data.repository.core.support。RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor .lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor .invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor。调用(DefaultMethodInvokingMethodInterceptor.java:59)~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)~ [spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) ~[spring-tx-5.1.9.RELEASE. jar:5.1.9.RELEASE] 在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org. springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor。调用(PersistenceExceptionTranslationInterceptor.java:139)~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)~[spring -aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.9.RELEASE.jar: 5.1.9.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework。 data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE] at org.springframework.aop.framework。ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~ [spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] at com.sun.proxy.$Proxy102.save(Unknown Source) ~[na:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)~[na:1.8.0_151] 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_151] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_151]在 java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.9 .RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation。invokeJoinpoint(ReflectiveMethodInvocation.java:198)~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)~[spring -aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.9.RELEASE.jar: 5.1.9.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework。 aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE] at com.sun.proxy.$Proxy102.save(Unknown Source) ~ [na:na] 在 com.giantmachines.demo.serviceinterfacesimpl。CreateDataService.createTimeSheet(CreateDataService.java:33) ~[classes/:na] at com.giantmachines.demo.controllers.TimeSheetController.createTimeSheetEntry(TimeSheetController.java:49) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl。调用0(本机方法)~[na:1.8.0_151] 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_151] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)~[na:1.8 .0_151] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring- web-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.9.RELEASE.jar :5.1.9.RELEASE] 在组织。springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.web.servlet .mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.web.servlet.mvc.method。 annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter .java:87) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)〜[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)〜[spring-webmvc-5.1.9 .RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]在 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 javax.servlet.http.HttpServlet.service( HttpServlet.java:660) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1 .9.RELEASE.jar:5.1.9.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org .apache。catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166 ) ~[tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0. 24.jar:9.0.24] 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache。 catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99 ) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.web.filter。OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~ [tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.24.jar: 9.0.24] 在 org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.web。 filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193 ) ~[tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina.core。ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring -web-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar: 5.1.9.RELEASE] 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina。 core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:151 ) ~[spring-session-core-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在 org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:86) ~[spring-session-core-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在 org.apache。 catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166 ) ~[tomcat-embed-core-9.0.24.jar:9.0.24] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.9.RELEASE. jar:5.1.9.RELEASE] 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE] 在 org. apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.24 .jar:9.0.24] 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina .core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [ tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.24.jar:9.0. 24] 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.catalina.core。StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.24.jar:9.0.24] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed -core-9.0.24.jar:9.0.24] 在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.24.jar:9.0.24] 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java: 860) [tomcat-embed-core-9.0.24.jar:9.0.24] 在 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) [tomcat-embed-core-9.0 .24.jar:9.0.24] 在 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.24.jar:9.0.24] 在 java。util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_151] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_151] at org.apache.tomcat.util。 thread.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.24.jar:9.0.24] 在 java.lang.Thread.run(Unknown Source) [na:1.8.0_151]

table: CREATE TABLE timesheet ( timesheet_entry_id BIGINT(5) auto_increment NOT NULL PRIMARY KEY, client VARCHAR(100), project VARCHAR(100), projcode VARCHAR(100), task VARCHAR(100), hours NUMERIC, hoursRounded VARCHAR(100), billable BOOLEAN, invoiced BOOLEAN, approved BOOLEAN, firstname VARCHAR(100), lastname VARCHAR(100), department VARCHAR(100), employee VARCHAR(100), billableRate NUMERIC, costRate NUMERIC, costAmount NUMERIC, currency VARCHAR(100), extRefURL VARCHAR (100));

标签: spring-bootspring-mvcspring-data-jpaspring-datapersistence

解决方案


使用 generation 类型策略,让 Hibernate 处理主键生成。

如果要自动递增主键,请使用下面。

@GeneratedValue(strategy=GenerationType.AUTO)

推荐阅读