Results 1 to 5 of 5

Thread: Simple job with JPA reader and writer

  1. #1

    Default Simple job with JPA reader and writer

    Hi,

    I have a simple job where I want to read from one database and write to another, and I want to use JPA to do this. Here is my config..

    Code:
    <job id="loadAdviceScheduleJob" xmlns="http://www.springframework.org/schema/batch">
    		<step id="step1">
    			<tasklet transaction-manager="transactionManager">
    				<chunk reader="AdviceScheduleReader" writer="AdviceScheduleWriter"
    					commit-interval="10" />
    				
    			</tasklet>
    		</step>
    	</job>
    	
    	<bean id="AdviceScheduleReader"
    		class="org.springframework.batch.item.database.JpaPagingItemReader">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    		<property name="queryString" value="select a from Adsphy01 a" />
    	</bean>
    
    	<bean id="AdviceScheduleWriter" class="org.springframework.batch.item.database.JpaItemWriter">
    		<property name="entityManagerFactory" ref="ISentityManagerFactory" />
    	</bean>
    My doubt is how do I tell JpaItemWriter which table and columns to write to? JpaPagingItemReader has a queryString property to specify the table and columns to read from, but JpaItemWriter does not seem to have any such property?

    Thanks in advance for any help.

  2. #2
    Join Date
    Dec 2010
    Posts
    175

    Default

    Create your own service which uses JPA implementation and use it from your custom writer. When the writer receives list of objects you can call your JPA service to persist the object.

  3. #3

    Default

    Hi tiger,

    So if I understand you correctly I should create my own custom writer which extends JpaItemWriter and then in the write method of that I should call EntityManagerFactory to create an EntityManager and then call that to persist the List of objects?

    Is there a code example you can give?

    Thanks much,
    Jahan

  4. #4

    Default

    Hi,

    I think I figured out a way to make this work by adding an ItemProcessor. In the processor, I take in an object of type from the reader and return an object for the type in the writer.

    Here is the code if anyone else is stuck on this....

    public class AdviceScheduleProcessor implements ItemProcessor<Adsphy01, AdviceSchedule>
    {

    public AdviceSchedule process(Adsphy01 item) throws Exception
    {
    AdviceSchedule isAdvice = new AdviceSchedule();
    isAdvice.setDescriptionOfSchedule(item.getDescripO fSchedule());
    return isAdvice;
    }

    }

  5. #5

    Default

    The ItemProcessor is the way to go. You can think of the ItemProcessor as a transformer. Take an object of one type and transform it into an object of another type. In this case the target object is a JPA annotated entity which is what the writer is expecting.

    Make sure you take a look at the samples provided in Spring Batch. They cover quite a few common use cases.

Posting Permissions

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