Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:42:51+00:00 2026-06-13T00:42:51+00:00

Using spring-batch and JPA (provided by hibernate). I have a step that does the

  • 0

Using spring-batch and JPA (provided by hibernate).

I have a step that does the following:

  • reads all clients from DB (Client entity)
  • enhances them with data from 3rd party. The ItemProcessor goes to the 3rd party data source, fetches some data that it stores in the Client entity itself (its fields) but also brings more data that is stored as different entities (ClientSale) and Client has a property of List which is mapped by ManyToOne.
  • The modified entity (Client) and the new ones (ClientSale) need to be stored in DB.

The reader part is straight forward, and for the writer I used JPAItemWriter. At the processing stage I tried to update the fields, create the new ones and add them to the client’s list and return the client, hoping that the writer will write both the referenced objects and the client itself to the DB.

Instead, I got an error saying that ClientSale with id #123213213 doesn’t exist in the DB.

How to I overcome this? Should I return a list of objects (different types) from my processor (the client + all ClientSale)? Can JPAItemWriter handle a list of objects? Another problem with that is that I’ll have to manually update the client_id in the ClientSale entities instead of adding them to the list and having hibernate to understand the relation between them and who points where..

What’s the best practice here?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:42:52+00:00Added an answer on June 13, 2026 at 12:42 am

    OK.. Here is what I did at the end based on everything:
    I created a MultiEntityItemWriter that can receive as item a list (and in that case it opens it and writes all elements to the delegated ItemWriter.

    Code:

    public class MultiEntityItemWriter implements ItemWriter<Object>{
    
    private ItemWriter<Object> delegate;
    
    @Override
    public void write(List<? extends Object> items) throws Exception {
        List<Object> toWrite = new ArrayList<>();
    
        for (Object item : items) {
            if (item instanceof Collection<?>) {
                toWrite.addAll((Collection<?>)item);
            } else {
                toWrite.add(item);
            }
        }
        delegate.write(toWrite);
    }
    
    
    public ItemWriter<Object> getDelegate() {
        return delegate;
    }
    
    public void setDelegate(ItemWriter<Object> delegate) {
        this.delegate = delegate;
    }
    

    }

    Now, my ItemProcessor can output a list with all entities to be written and I don’t need to rely on JPA to understand that there are more entities to be committed to the DB.

    hope it helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Spring Batch. I have following question. I am using Spring
Background I have a Spring batch program that reads a file (example file I
I have created a spring-batch job. My reader class reads the data from the
I'm using Spring Batch for a system that does a lot of batch operations.
I have a spring batch program which reads from a file and writes to
I'm using hibernate JPA (without Spring) and it's working well, but I have come
I'm using Spring Batch 2.1.5. I have many jobs that are very similar between
I am using Spring 3.0 with Hibernate and PostgreSQL and I have following problem:
I am using a custom MVC application that has dependencies on SPring Batch as
I have a com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException in my code (using Hibernate and Spring) and I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.