org.h2.jdbc.JdbcSQLException: NULL не допускается для столбца "ID";Проблема оператора SQL с идентификатором @GeneratedValue (стратегии = GenerationType.IDENTITY) - PullRequest
0 голосов
/ 03 декабря 2018

Эта проблема возникает при попытке сохранить объект в БД.

org.h2.jdbc.JdbcSQLException: NULL не разрешено для столбца "ID";Оператор SQL: вставьте в учетные данные (id, fisicHost_id, заметки, пароль, роль, пользователь) значения (null,?,?,?,?,?) [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.executeUbdjj_jp_jp (jp)h2-1.4.191.jar: 1.4.191] at 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.let.(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 (AbstractReturnjDate)) ~ [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] в орг.hibernate.event.internal.AbstractSaveEventListener.performSave (AbstractSaveEventListener.java:178) ~ [hibernate-core-5.0.6.Final.jar: 5.0.6.Final] в org.hibernate.event.internal.AbstractSaveEventListener.saveWith AbstracterstainEvent (.java: 109) ~ [hibernate-core-5.0.6.Final.jar: 5.0.6.Final] в org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId (DefaultSaveOrUpdateEventListener.java:192-core- hiber-192) [h5.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] at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient (DefaultSaveOrUpdateEventListener.java:177) ~ [hibernate-core-5.0.6.Final.jar: 5.0.6.Final] в org.hibernate.ListenSeventEe.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.SmpI.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 ():нет] в 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.jpg) ~ [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] at org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept (CglibAopProxy.java:656) ~ [spring-aop-4.3.5.RELEASE.jar: 4.3.5.RELASE] в com.amco.Datacenter.dao.CredentialDaoImpl $$ EnhancerBySpringCGLIB $$ c7ae50e6.save () ~ [main /: na] в com.amco.Datacenter.service.CredentialServiceImpl.save (CredentialServiceImpl34] [mainden []/: na] at com.amco.Datacenter.controller.Controlador.addCredential (Controlador.java:45) ~ [main /: na] at sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод) ~ [na: 1.8.0_121]на солнцеrefle.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) ~ [na: 1.8.0_121] в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) ~1 на.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] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod.java:134) ~ [spring-web-4.3.5.RELEASE.jar: 4.3.5.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle (ServletInvocableHandlerMethod.java:116) ~ [spring-webmvc-4.3.5.RELEASE.jar.sra.ReR.Ref.ReR.ReR.ReR.ReR.ReR.R.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod (RequestMappingHandlerAdapter.java:827) ~ [spring-webmvc-4.3.5.RELEASE.jar: 4.3.5.RELEASE] в org.springfraletwork.we.c.method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapter.java:738) ~ [spring-webmvc-4.3.5.RELEASE.jar: 4.3.5.RELEASE] в org.springframework.web.servlet.mvc.hodeMaster.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] at 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.serlet (HttpServServ.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] в орг.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:230) ~ [tomcat-embed-core-8.5.6.jar: 8.5.6] в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterCha: 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] в орг.pache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:165) ~ [tomcat-embed-core-8.5.6.jar: 8.5.6] в org.springframework.web.filter.RequestContextFilter.doFilterFternal (RequestConte): 99) ~ [spring-web-4.3.5.RELEASE.jar: 4.3.5.RELEASE] at 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.doFilterIndoFilter.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] at 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] at 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.java:192)) ~ [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] at 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 (ApplicationFilterCha2 ~):[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.служба (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 в]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]

Это класс Credential с автоматически сгенерированным идентификатором (не могу понять, почему он нулевой)

@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();
}

Я отладил программу, и значения ожидаемые, проблемав том, что учетные данные не сохраняются должным образом из-за этой проблемы с нулевым идентификатором, которую я не могу догадаться, почему это происходит.

Есть идеи?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...