Results 1 to 4 of 4

Thread: Delete a table with an entitymanager in a tasklet

  1. #1
    Join Date
    Aug 2008
    Posts
    6

    Default 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. #2
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    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.

  3. #3
    Join Date
    Jun 2010
    Posts
    5

    Default

    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 at 11:21 AM.

  4. #4
    Join Date
    Dec 2006
    Posts
    1,061

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •