Nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

37,367

You said Hibernate that the ID was autogenerated using the strategy AUTO. This strategy, with MySQL, consists in letting the database generate the ID using an auto-increment column. So if you haven't defined uid as auto-increment, that won't work.

Share:
37,367
Vindaloo
Author by

Vindaloo

Updated on July 18, 2020

Comments

  • Vindaloo
    Vindaloo almost 4 years

    I was kind of roped into using maven, Hibernate, spring and JPA repositories in a small project. I cheerfully assumed that information found here

    http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html

    will be enough to create at least a simple application. Unluckily that's not the case. I've managed to read data from a MySQL database, but as for now have been unable to insert any data.

    Here is a simple 'User' model class.

    @Entity
    @Table(name = "ctuser")
    public class User implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long uid;
    
        @Column(name = "Gmail", nullable = false)
        private String gmail;
    
    
        public String getGmail() {
            return gmail;
        }
    
        public void setGmail(String gmail) {
            this.gmail = gmail;
        }
    
        @Override
        public String toString() {
            return getGmail();
        }
    }
    

    I've created an appropriate repository interface...

    package put.io.come_together.service.persistance.repository;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import put.io.come_together.model.User;
    
    public interface UserRepository extends JpaRepository<User, Long> {
    
        User findUserByUid(long uid);
    
        User save (User obj);
    
        }
    

    ...an interface for the service...

    public interface UserService {
    
        User getUser(long uid);
    
        User addUser(User obj);
    
        List<User> getAll();
    
    }
    

    ...and implementation of the interface.

    @Service
    public class UserServiceImpl implements UserService {
        private final UserRepository userRepository;
    
        @Autowired
        public UserServiceImpl(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    
        @Override
        public User getUser(long uid) {return userRepository.findUserByUid(uid);
        }
    
        @Transactional
        @Override
        public User addUser(User obj) {return this.userRepository.saveAndFlush(obj);}
    
        @Override
        public List<User> getAll() {
            return userRepository.findAll();
        }
    

    Finally I'm trying to present as the data as well as insert something into database.

    @Controller
    public class HomePageController {
    
    
        @Autowired
        private UserService userService;
    
        private static final String USER_MAP_KEY = "groups";
        private static final String USER_LIST_TEMPLATE_NAME = "groupList";
    
        @RequestMapping("/")
        String hello(Map<String, Object> model) {
            User osoba = new User();
            osoba.setGmail("ADAMWEST");
            userService.addUser(osoba);
    
            List<User> userGroupsList = userService.getAll();
            model.put(USER_MAP_KEY, userGroupsList);
            return USER_LIST_TEMPLATE_NAME;
        }
    
    }
    

    If I leave out the userService.addUser(osoba); line, everything works fine. I can see the data read from the database neatly laid out. But when I try to actually use the .addUser() method my localhost puts out a white label error page.

    White label Error Page

    This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Apr 19 09:36:52 CEST 2015 There was an unexpected error (type=Internal Server Error, status=500). could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

    Would you kindly tell me what is it that I'm doing wrong? I've been trying to solve the problem for some time now, and thoroughly lost hope.

    Here is the stack trace of the exception. The error concerns field 'UID' not having a default value, but I marked it as an @Id field. I also dropped the entire database and set it up again, seeing as it helped someone with a similar problem.

    java.sql.SQLException: Field 'UID' doesn't have a default value
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
            at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
            at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
            at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530)
            at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062)
            at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
            at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
            at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
            at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
            at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
            at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
            at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)
            at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
            at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
            at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
            at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
            at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:
    288)
            at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
            at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125
    )
            at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.jav
    a:84)
            at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:2
    06)
            at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
            at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
            at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
            at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
            at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
            at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:497)
            at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(Extend
    edEntityManagerCreator.java:344)
            at com.sun.proxy.$Proxy78.persist(Unknown Source)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:497)
            at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEnti
    tyManagerCreator.java:291)
            at com.sun.proxy.$Proxy78.persist(Unknown Source)
            at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:407)
            at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:421
    )
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:497)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.exec
    uteMethodOn(RepositoryFactorySupport.java:416)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doIn
    voke(RepositoryFactorySupport.java:401)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invo
    ke(RepositoryFactorySupport.java:373)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodIntercep
    tor.invoke(RepositoryFactorySupport.java:486)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterce
    ptor.java:99)
            at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspec
    tSupport.java:281)
            at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTransla
    tionInterceptor.java:136)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingM
    ethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
            at com.sun.proxy.$Proxy80.saveAndFlush(Unknown Source)
            at put.io.come_together.service.persistance.impl.UserServiceImpl.addUser(UserServiceImpl.java:26)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:497)
            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:
    190)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
            at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterce
    ptor.java:99)
            at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspec
    tSupport.java:281)
            at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
            at com.sun.proxy.$Proxy83.addUser(Unknown Source)
            at put.io.come_together.controller.HomePageController.hello(HomePageController.java:29)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:497)
            at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
            at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:13
    7)
            at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletIn
    vocableHandlerMethod.java:110)
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(Request
    MappingHandlerAdapter.java:777)
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMapp
    ingHandlerAdapter.java:706)
            at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.j
    ava:85)
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
            at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
            at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
            at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
            at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
            at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
            at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
            at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            at java.lang.Thread.run(Thread.java:745)
    • JB Nizet
      JB Nizet about 9 years
      Post the complete stack trace of the exception.
    • flup
      flup about 9 years
      And set debug log level.
    • Siebz
      Siebz about 9 years
      if you dont mind the UID auto incrementing just delete the strategy then it should run. For more info on strategies look here stackoverflow.com/questions/10041938/…