Announcement Announcement Module
Collapse
No announcement yet.
Delete a table with an entitymanager in a tasklet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete a table with an entitymanager in a tasklet

    Hi,

    I try to delete a table with an EntityManager from hibernate.

    My step implement taskletStep :
    Code:
    				<bean id="step1" parent="taskletStep" >
    					<property name="tasklet"
    					ref="baseAdministrativeSupprimeTasklet" />
    					<property name="jobRepository" ref="jobRepository" />
    				</bean>
    
    <bean id="baseAdministrativeSupprimeTasklet"
    		class="com.natixis.facturationnie.batch.writer.BaseAdministrativeSupprimeTasklet">
    My class is:

    Code:
    public class BaseAdministrativeSupprimeTasklet implements Tasklet, InitializingBean {
     
     protected EntityManager entityManager;
     
     public ExitStatus execute() throws Exception { 
    	  Query lquery = entityManager.createNativeQuery("delete from fac_maj_base_adm");
       
       lquery.executeUpdate();
     
     
      return ExitStatus.FINISHED;
     }
    When the progamm call the "lquery.executeUpdate();", it throws a
    javax.persistence.TransactionRequiredException: Executing an update/delete query

    There is no transaction started.

    When I whant to do a select, I implement "SimpleStep" in the property file and
    ItemReader in the class, it works fine!

    The configuration of hibernate is:
    Code:
    	<persistence-unit name="applicationEntityManagerBatch" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<properties>
    
    			<property name="hibernate.dialect" value="org.hibernate.dialect.Sybase11Dialect" />
    			<property name="hibernate.query.substitutions" value="true 'Y', false 'N'" />
    			<property name="hibernate.show_sql" value="true" />
    			<property name="hibernate.use_sql_comments" value="true" />
    			<property name="hibernate.format_sql" value="true" />
    			<property name="hibernate.cache.use_second_level_cache" value="true" />
    			<property name="hibernate.cache.use_query_cache" value="true" />
    			<property name="hibernate.cache.provider_class"	value="org.hibernate.cache.EhCacheProvider" />
    			<property name="hibernate.jdbc.batch_size" value="10000" />
    			<property name="hibernate.max_fetch_depth" value="0" />
    			<property name="hibernate.order_inserts" value="true" />
    			<property name="hibernate.order_updates" value="true" />
                
    		</properties>
    	</persistence-unit>


    Why the transaction isn't started with the tasklet?
    Have you a way to delete table with an entity manager?

    Thanks in advance,
    Sylvain

  • #2
    The SimpleStep automatically handles transactions, because it has to be very specific about programmatic control of them. The Tasklet however, has no such requirement. Therefore, if you need the actions done in the tasklet to be in a Transaction, you need to add them yourself. I would recommend wrapping it using a Transaction advice, as you would with a normal hibernate DAO.

    Comment


    • #3
      Hello, wriggle.

      Have you solved your problem?

      I met the same one.

      tried :

      Code:
      EntityManager em = entityManagerFactory.createEntityManager();
      EntityTransaction transaction = em.getTransaction();
      
      transaction.begin();
      --myQuery
      transaction.commit();
      em.close();
      entityManagerFactory.close();

      thank you in advance
      --
      sergionni
      Last edited by sergionni; Jun 3rd, 2010, 12:21 PM.

      Comment


      • #4
        Interesting. Do you have the same problem when using a JdbcTemplate?

        Comment

        Working...
        X