org.h2.jdbc.JdbcSQLException:列“ ID”不允许为NULL; @GeneratedValue(strategy = GenerationType.IDENTITY)id

时间:2018-12-03 20:28:34

标签: java spring hibernate spring-mvc

尝试在数据库中保存对象时遇到此问题。

  

org.h2.jdbc.JdbcSQLException:列“ ID”不允许为NULL;的SQL   声明:插入凭据(id,fisicHost_id,注释,密码,   角色,用户)值(空,?,?,?,?,?)[23502-191]   org.h2.engine.SessionRemote.done(SessionRemote.java:624)   〜[h2-1.4.191.jar:1.4.191]在   org.h2.command.CommandRemote.executeUpdate(CommandRemote.java:191)   〜[h2-1.4.191.jar:1.4.191]在   org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:160)   〜[h2-1.4.191.jar:1.4.191]在   org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:146)   〜[h2-1.4.191.jar:1.4.191]在   org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:97)   〜[tomcat-dbcp-8.0.32.jar:8.0.32]在   org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:97)   〜[tomcat-dbcp-8.0.32.jar:8.0.32]在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.id.IdentityGenerator $ GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:84)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2792)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3362)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)   〜[hibernate-core-5.0.6.Final.jar:5.0.6.Final]在   com.amco.Datacenter.dao.CredentialDaoImpl.save(CredentialDaoImpl.java:55)   〜[main /:na]在   com.amco.Datacenter.dao.CredentialDaoImpl $$ FastClassBySpringCGLIB $$ 67cc97d9.invoke()   〜[main /:na]在   org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)   〜[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)   〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)   〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)   〜[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)   〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)   〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   com.amco.Datacenter.dao.CredentialDaoImpl $$ EnhancerBySpringCGLIB $$ c7ae50e6.save()   〜[main /:na]在   com.amco.Datacenter.service.CredentialServiceImpl.save(CredentialServiceImpl.java:34)   〜[main /:na]在   com.amco.Datacenter.controller.Controlador.addCredential(Controlador.java:45)   〜[main /:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)〜[na:1.8.0_121]在   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_121]在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_121]在java.lang.reflect.Method.invoke(Method.java:498)   〜[na:1.8.0_121]在   org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   javax.servlet.http.HttpServlet.service(HttpServlet.java:648)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)   〜[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)   〜[tomcat-embed-websocket-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)   〜[tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:802)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1410)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)   [na:1.8.0_121]在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)   [na:1.8.0_121]在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)   [tomcat-embed-core-8.5.6.jar:8.5.6]在   java.lang.Thread.run(Thread.java:745)[na:1.8.0_121]

这是具有自动生成ID的凭据类(无法猜测为什么它为空)

@Entity
public class Credential {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @JsonIgnore
    @ManyToOne(fetch= FetchType.LAZY)
    private FisicHost fisicHost;

    private String user;
    private String password;
    private String notes;
    private String role;

    public Credential(){

    }

    public Credential(int id, FisicHost fisicHost, String user, String password, String notes, String role) {
        this.id = id;
        this.fisicHost = fisicHost;
        this.user = user;
        this.password = password;
        this.notes = notes;
        this.role = role;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public FisicHost getFisicHost() {
        return fisicHost;
    }

    public void setFisicHost(FisicHost fisicHost) {
        this.fisicHost = fisicHost;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

这是处理请求的控制器方法:

// Método para agregar credenciales
@RequestMapping(value = "/fisicHost/{id}/credentials/add", method = RequestMethod.POST)
public void addCredential(@PathVariable(value = "id") int fisicHostId, @RequestParam(value = "User") String user,
                          @RequestParam(value = "Password") String password, @RequestParam(value = "Notes") String notes){
    FisicHost fisicHost = fisicHostDao.findById(fisicHostId);
    List<Credential> allCredentials = fisicHost.getCredentials();
    Credential credential = new Credential();
    credential.setUser(user);
    credential.setPassword(password);
    credential.setNotes(notes);
    credentialService.save(credential);
}

这是CredentialDaoImpl.java保存方法的编码方式:

@Override
public void save(Credential credential) {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(credential);
    session.getTransaction().commit();
    session.close();
}

我调试了程序,并且值是预期值,问题是由于此null id问题导致凭据无法正确保存,我无法猜测为什么会发生这种情况。

有什么想法吗?

0 个答案:

没有答案