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

  • SEARCH
  • Home
  • 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 6195481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:28:34+00:00 2026-05-24T03:28:34+00:00

I’ve been using Hibernate to store a parent-child relationship using @OneToMany with an @JoinColumn

  • 0

I’ve been using Hibernate to store a parent-child relationship using @OneToMany with an @JoinColumn for some time, it has worked great.

But now I’ve reached a point where the total size of the objects is just too big to fit in memory. (e.g. there are 3 million child records now). The records are all stored in a file, then parsed into Java objects before being hibernated.

I’d like to “chunk” or “batch” the records so that I’ll only need to read a fraction of them into memory at a time. My approach is something like “load collection of 10,000 children objects, persist to database (calling ‘update’ on parent obj), empty out children collection to free up RAM, repeat”.

I want this to work like:

Iteration 1:  Chunk1 (records 1-10,000) stored
Iteration 2:  Chunk2 (records 10,001-20,000) stored
Iteration 3:  Chunk3 (records 20,001-30,000) stored
etc

Here’s where I’m having trouble. The collection I’m saving changes with each iteration, which causes hibernate to drop all the old children before saving the new children. Instead of getting all my chunks saved, I end up with

Iteration 1: Chunk1 stored
Iteration 2: Chunk1 objects deleted, Chunk 2 stored
Iteration 3: Chunk2 objects deleted, Chunk 3 stored
etc

So in the end, only my final chunk is saved.

Is there any way to change this behavior? I have read about JDBC batching but that’s not quite what I’m looking for. I’ve also tried storing each Child separately, instead of via an “update” to the parent, but when I do this the Child records are persisted without a pointer to their parent.

Update:

Thanks for the speedy and terrific response. The relationship is not bidirectional — I will try to make it that way. I have legacy code that won’t cooperate with schema changes so am a little constrained.

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-05-24T03:28:34+00:00Added an answer on May 24, 2026 at 3:28 am

    The main question here is: is your relationship bi-directional? That is, do you have a @ManyToOne on the child side pointing back to parent?

    If you do, that relationship needs to be owned by the child side:

    // in Parent
    @OneToMany(mappedBy="parent")
    List<Child> getChildren();
    
    // in Child
    @ManyToOne
    Parent getParent();
    

    When set up this way, you do NOT need to load all (or any, for that matter) children in your parent’s collection – you can instead load (or create) children, set parent on their end and save them; you can certainly do that in batches.

    If your relationship is not bi-directional then based on your question it’s owned by the parent – you’ll then need to make it bi-directional as shown above (or uni-directional, but opposite to what it is now – see below).

    On a somewhat separate note, do you even need a @OneToMany on parent side here? With 3 million children records I can’t really imagine where it’d be useful.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.