Announcement Announcement Module
Collapse
No announcement yet.
Simple job with JPA reader and writer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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;
        }

        }

        Comment


        • #5
          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.

          Comment

          Working...
          X