title: javax.persistence.TransactionRequiredException: Executing an update/delete query
body: <p>I am using JPA2.0+hibernate3.2+Spring3.0.5+MySql5.5 to implement DAO function,but it doesn't work and just throw javax.persistence.TransactionRequiredException when i tried to persist entity to DB.  Please see my coding and configuration.</p>
  
  <p>1.Entity </p>
  
  <pre><code>@Entity
  @Table(name="booking_no")
  public class BookingNo {
      public BookingNo(){
      };
      @Id
      @GeneratedValue
      private Integer id;
      @Column(unique=true,length=30)
      private String prefix;
  </code></pre>
  
  <p>2.DAO</p>
  
  <pre><code>@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
      public Object generateBookingNo(String customer) throws Exception{
          logger.debug("generateBookingNo() start,generate booking no by customer:"+customer);
          if(customer == null || customer.trim().length() == 0){
              logger.error("generateBookingNo(),customer is empty,return null");
              return null;
          }
          EntityManager em = emf.createEntityManager();
          try{
                  Query query = em.createQuery("select b from BookingNo b where b.prefix='"+customer+"'");
                  Object object =null;
                  try{
                          object = query.getSingleResult();
                  }catch(NoResultException e){
                          logger.info("generateBookingNo(),not find id for customer["+customer+"],will save a initial record");
                          BookingNo bkNo = new BookingNo();
                          bkNo.setPrefix(customer);
                          logger.debug("generateBookingNo(),the bookingNo is:"+bkNo);
                          em.persist(bkNo);
                          //em.flush();
                          Object object2 =null;
                          try{
                                 object2 = em.createQuery("select b.id from BookingNo b where b.prefix='"+customer+"'").getSingleResult();
                          }catch(Exception e2){
                              logger.error("get error when query customer ["+customer+"]",e);
                              return null;
                          }
                          return  customer+"-"+object2;
                  }
                  if(object == null || !(object instanceof BookingNo)){
                      logger.error("generateBookingNo(),return nothing but not catch NoResultException,return null");
                      return null;
                  }
                  BookingNo bkNo =(BookingNo) object;
                  Integer newId = bkNo.getId()+1;
                  //Query query2 = em.createQuery("update BookingNo b set b.id="+newId+" where b.prefix='"+customer+"'");
                  Query query2 = em.createNativeQuery("update booking_no b set b.id="+newId+" where b.prefix='"+customer+"'");
                  int res = query2.executeUpdate();
                  logger.debug("generateBookingNo(),the to be update bookingNo is:"+bkNo+",the update result is:"+res);
                  //em.flush();
                  return customer+"-"+newId;
          }catch(Exception e){
              logger.error("get error in generateBookingNo()",e);
              return null;
          }finally{
              em.close();
              emf.close();
          }
  </code></pre>
  
  <p>3.Spring cfg file</p>
  
  <pre><code>    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;  
      &lt;property name="entityManagerFactory" ref="bookingEMF"/&gt;  
  &lt;/bean&gt;  
       &lt;bean id="dataSource1"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
                 &lt;property name="driverClass" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt;
                 &lt;property name="jdbcUrl" value="jdbc:mysql://localhost/booking"&gt;&lt;/property&gt;
                 &lt;property name="user" value="root"&gt;&lt;/property&gt;
                 &lt;property name="password" value="123456"&gt;&lt;/property&gt;
                 &lt;property name="initialPoolSize" value="1"&gt;&lt;/property&gt;
                 &lt;property name="minPoolSize" value="1"&gt;&lt;/property&gt;
                 &lt;property name="maxPoolSize" value="20"&gt;&lt;/property&gt;
                 &lt;property name="maxIdleTime" value="60"&gt;&lt;/property&gt;
                 &lt;property name="acquireIncrement" value="5"&gt;&lt;/property&gt;
                 &lt;property name="idleConnectionTestPeriod" value="60"&gt;&lt;/property&gt;
                 &lt;property name="acquireRetryAttempts" value="20"&gt;&lt;/property&gt;
                 &lt;property name="breakAfterAcquireFailure" value="true"&gt;&lt;/property&gt;
  &lt;/bean&gt;
      &lt;!-- Entity Manager Factory --&gt;
       &lt;bean id="bookingEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
                  &lt;property name="persistenceUnitName" value="booking"/&gt;  
                  &lt;property name="dataSource" ref="dataSource1" /&gt;
                  &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
                  &lt;property name="jpaPropertyMap"&gt;  
                     &lt;map&gt;  
                           &lt;entry key="hibernate.transaction.flush_before_completion"     value="true"/&gt; 
                           &lt;entry key="hibernate.transaction.auto_close_session"  value="true"/&gt;  
                           &lt;entry key="hibernate.connection.release_mode" value="auto"/&gt; 
                            &lt;entry key="hibernate.hbm2ddl.auto" value="update"/&gt; 
                            &lt;entry key="format_sql" value="true"/&gt; 
                           &lt;!-- &lt;entry key="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/&gt;   --&gt;
                     &lt;/map&gt;  
                  &lt;/property&gt; 
      &lt;/bean&gt;
      &lt;!-- JPA Vendor,Implementation is hibernate --&gt;
       &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="database" value="MYSQL" /&gt;
                  &lt;property name="showSql" value="true"/&gt;
                  &lt;property name="generateDdl" value="false"/&gt;
  
                  &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt;
      &lt;/bean&gt;
      &lt;!-- DAO --&gt;
      &lt;bean id="BookingDAO" class="com.chailie.booking.dao.impl.booking.BookingDAO" &gt;
                  &lt;property name="emf" ref="bookingEMF"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>when run junit test case as following</p>
  
  <pre><code>@Test
      public void testGenerateBookingNo(){
          try {
              BookingDAO dao  = (BookingDAO) DAOFactory.getDAO("BookingDAO", DAOFactory.TYPE_APPLICATION);
              dao.generateBookingNo("chailie");
              //dao.generateBookingNo("chailie2");
          } catch (Exception e) {
              // TODO Auto-generated catch block
              logger.error("get error",e);
          }
  
      }
  </code></pre>
  
  <p>It will occur javax.persistence.TransactionRequiredException and i don't know what happeded,does anybody could help to solve this?i really appreciate it
  By the way,please see my log</p>
  
  <pre><code>23:15:46.817 [main] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
  23:15:46.837 [main] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: select b from com.chailie.booking.model.booking.BookingNo b where b.prefix='chailie'
  23:15:46.837 [main] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: select bookingno0_.id as id0_, bookingno0_.prefix as prefix0_ from booking_no bookingno0_ where bookingno0_.prefix='chailie'
  23:15:46.837 [main] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
  23:15:46.856 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
  23:15:46.856 [main] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
  23:15:46.891 [main] DEBUG org.hibernate.SQL - select bookingno0_.id as id0_, bookingno0_.prefix as prefix0_ from booking_no bookingno0_ where bookingno0_.prefix='chailie' limit ?
  Hibernate: select bookingno0_.id as id0_, bookingno0_.prefix as prefix0_ from booking_no bookingno0_ where bookingno0_.prefix='chailie' limit ?
  23:15:46.922 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
  23:15:46.926 [main] DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.chailie.booking.model.booking.BookingNo#8]
  23:15:46.935 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
  23:15:46.936 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
  23:15:46.936 [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
  23:15:46.936 [main] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
  23:15:46.939 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [com.chailie.booking.model.booking.BookingNo#8]
  23:15:46.941 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [com.chailie.booking.model.booking.BookingNo#8]
  23:15:46.942 [main] DEBUG o.h.e.StatefulPersistenceContext - initializing non-lazy collections
  23:15:46.942 [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
  23:15:46.944 [main] DEBUG o.h.ejb.AbstractEntityManagerImpl - mark transaction for rollback
  23:15:46.954 [main] ERROR c.c.b.d.booking.impl.BookingDAOTest - get error in generateBookingNo()
  javax.persistence.TransactionRequiredException: Executing an update/delete query
      at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:48) [hibernate-entitymanager-3.4.0.GA.jar:3.4.0.GA]
      at com.chailie.booking.dao.impl.booking.BookingDAO.generateBookingNo(BookingDAO.java:104) [classes/:na]
      at com.chailie.booking.dao.impl.booking.BookingDAO$$FastClassByCGLIB$$2898182b.invoke(&lt;generated&gt;) [cglib-2.2.jar:na]
      at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) [cglib-2.2.jar:na]
      at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688) [spring-aop-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) [spring-tx-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621) [spring-aop-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at com.chailie.booking.dao.impl.booking.BookingDAO$$EnhancerByCGLIB$$58d6a935.generateBookingNo(&lt;generated&gt;) [cglib-2.2.jar:na]
      at com.chailie.booking.dao.booking.impl.BookingDAOTest.testGenerateBookingNo(BookingDAOTest.java:42) [test-classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.7.0_15]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [na:1.7.0_15]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [na:1.7.0_15]
      at java.lang.reflect.Method.invoke(Unknown Source) [na:1.7.0_15]
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) [junit-4.7.jar:na]
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) [junit-4.7.jar:na]
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) [junit-4.7.jar:na]
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) [junit-4.7.jar:na]
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) [junit-4.7.jar:na]
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) [junit-4.7.jar:na]
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) [junit-4.7.jar:na]
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) [junit-4.7.jar:na]
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) [junit-4.7.jar:na]
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) [junit-4.7.jar:na]
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) [junit-4.7.jar:na]
      at org.junit.runners.ParentRunner.run(ParentRunner.java:236) [junit-4.7.jar:na]
      at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) [surefire-junit4-2.10.jar:2.10]
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) [surefire-junit4-2.10.jar:2.10]
      at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) [surefire-junit4-2.10.jar:2.10]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.7.0_15]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [na:1.7.0_15]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [na:1.7.0_15]
      at java.lang.reflect.Method.invoke(Unknown Source) [na:1.7.0_15]
      at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) [surefire-api-2.10.jar:2.10]
      at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) [surefire-booter-2.10.jar:2.10]
      at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) [surefire-booter-2.10.jar:2.10]
      at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) [surefire-booter-2.10.jar:2.10]
      at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) [surefire-booter-2.10.jar:2.10]
  23:15:46.958 [main] INFO  o.hibernate.impl.SessionFactoryImpl - closing
  23:15:46.960 [main] DEBUG o.h.transaction.JDBCTransaction - commit
  23:15:46.962 [main] DEBUG o.h.transaction.JDBCTransaction - re-enabling autocommit
  23:15:46.962 [main] DEBUG o.h.transaction.JDBCTransaction - committed JDBC Connection
  23:15:46.962 [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
  23:15:46.963 [main] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
  </code></pre>