4 Replies Latest reply on Mar 25, 2007 10:27 AM by suneetshah

    Problem persisting data using Spring + Hibernate JPA

      Hello,

      I am getting the following exception when I try to insert a record into mysql. I am using Spring 2.03 with Hibernate JPA. I have pasted my code below and the config files.

      Any help would be greatly appreciated.

      Suneet

      ---- service class ----

      public class ServiceMgr {

      protected ServiceDAO serviceDao;

      public ServiceMgr() { }

      public ServiceMgr(ServiceDAO serviceDao) {
      super();
      this.serviceDao = serviceDao;
      }


      public Service getService(String id) {
      return serviceDao.findById(id);
      }


      @Transactional( propagation = Propagation.REQUIRED, readOnly = false )
      public void addService(Service serv) {
      serviceDao.persist(serv);
      }

      ---- DOA Impl class ----


      public class ServiceDAOBean implements ServiceDAO {

      private static final Log log = LogFactory.getLog(ServiceDAOBean.class);

      private EntityManager entityManager;
      private EntityManagerFactory emf;


      @PersistenceContext
      public void setEntityManager(EntityManager em) {
      entityManager = em;
      }
      @PersistenceContext
      public void setEntityManagerFactory(EntityManagerFactory emf) {
      this.emf = emf;
      }


      @Transactional( propagation = Propagation.REQUIRED, readOnly = false )
      public void persist(Service transientInstance) {
      log.debug("persisting Service instance");
      try {
      entityManager.persist(transientInstance);
      log.debug("persist successful");
      } catch (RuntimeException re) {
      log.error("persist failed", re);
      throw re;
      }
      }


      ---- Spring applicationContext.xml ----
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">



      <constructor-arg ref="serviceDAO"></constructor-arg>































      <tx:annotation-driven transaction-manager="transactionManager" />






        • 1. Re: Problem persisting data using Spring + Hibernate JPA

          The xml file did not paste properly:

          <bean id="serviceManager" class="diamelle.common.service.ServiceMgr"
           <constructor-arg ref="serviceDAO"></constructor-arg>
           </bean>
          
           <bean id="serviceDAO" class="diamelle.common.service.ServiceDAOBean" >
           <property name="entityManagerFactory" ref="entityManagerFactory" />
           </bean>
          
          
           <bean id="entityManagerFactory"
           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
           <property name="dataSource" ref="dataSource" />
           <property name="jpaVendorAdapter">
           <bean
           class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="generateDdl" value="false" />
           <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
           <property name="showSql" value="true" />
           </bean>
           </property>
           </bean>
          
           <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver" />
           <property name="url" value="jdbc:mysql://localhost:3306/mysql" />
           <property name="username" value="idmuser" />
           <property name="password" value="idmuser" />
           </bean>
          
           <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
           <property name="dataSource" ref="dataSource" />
           <property name="entityManagerFactory" ref="entityManagerFactory" />
           </bean>
          
           <tx:annotation-driven transaction-manager="transactionManager" />
           <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
           <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
          
          
          
           </beans>


          • 2. Re: Problem persisting data using Spring + Hibernate JPA

            The exception is pasted below.
            Thanks.


            org.springframework.transaction.UnexpectedRollbackException: JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation); nested exception is javax.persistence.RollbackException: Error while commiting the transaction
            Caused by: javax.persistence.RollbackException: Error while commiting the transaction
            at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
            at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:427)
            at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:651)
            at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:621)
            at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:311)
            at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
            at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:628)
            at diamelle.common.service.ServiceMgr$$EnhancerByCGLIB$$2f734b75.addService()
            at test.diamelle.common.service.ServiceMgrTest.testAddService(ServiceMgrTest.java:44)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at junit.framework.TestCase.runTest(TestCase.java:164)
            at junit.framework.TestCase.runBare(TestCase.java:130)
            at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
            at junit.framework.TestResult$1.protect(TestResult.java:110)
            at junit.framework.TestResult.runProtected(TestResult.java:128)
            at junit.framework.TestResult.run(TestResult.java:113)
            at junit.framework.TestCase.run(TestCase.java:120)
            at junit.framework.TestSuite.runTest(TestSuite.java:228)
            at junit.framework.TestSuite.run(TestSuite.java:223)
            at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
            Caused by: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
            at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
            at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
            at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
            at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
            at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
            at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
            at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
            at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
            at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
            at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
            at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
            ... 29 more
            Caused by: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.service (STATUS, SERVICE_NAME, LOCATION_IP_ADDRESS, COMPANY_OWNER_ID, START_DAT' at line 1
            at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:896)
            at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
            at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
            ... 37 more

            ----------Generated SQL:----------------------
            Hibernate: insert into mysql.mysql.service (STATUS, SERVICE_NAME, LOCATION_IP_ADDRESS, COMPANY_OWNER_ID, START_DATE, END_DATE, LICENSE_KEY, SERVICE_TYPE, PARENT_SERVICE_ID, ROOT_RESOURCE_ID, ACCESS_CONTROL_MODEL, PARAM1, PARAM2, PARAM3, PARAM4, PARAM5, SERVICE_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

            • 3. Re: Problem persisting data using Spring + Hibernate JPA
              alesj

               

              "suneetshah" wrote:

              Caused by: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.service (STATUS, SERVICE_NAME, LOCATION_IP_ADDRESS, COMPANY_OWNER_ID, START_DAT' at line 1
              at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:896)
              at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
              at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
              ... 37 more

              ----------Generated SQL:----------------------
              Hibernate: insert into mysql.mysql.service (STATUS, SERVICE_NAME, LOCATION_IP_ADDRESS, COMPANY_OWNER_ID, START_DATE, END_DATE, LICENSE_KEY, SERVICE_TYPE, PARENT_SERVICE_ID, ROOT_RESOURCE_ID, ACCESS_CONTROL_MODEL, PARAM1, PARAM2, PARAM3, PARAM4, PARAM5, SERVICE_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


              • 4. Re: Problem persisting data using Spring + Hibernate JPA

                Thanks for the tip. The problem was with the annotation on the service class that was generated by the hibernate tool.

                @Entity
                @Table(name = "service", catalog = "mysql", uniqueConstraints = {})

                When I got rid of the catalog tag, everything started to work.

                Thanks for the tip.

                Regards
                Suneet