Having a parent and child class as below
@Entity
class Parent {
@Id
Long id;
@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
List<Child> children;
}
@Embeddable
class Child{
@Id
Long id;
// child does not have parent id
}
I am using ObjectDB and JPA. My db got bigger and some parents has 500K children. Normally, to get all children of a parent, I was loading parent and accessing children as parent.getChildren() via lazy loading. However, since the list too big, it requires a lot of memory.
How can I get all children of a specific parent as a lightweight DTO object list in a performant way?
Bonus question: how can I delete all children of a parent efficiently?
Setting Child as embeddable is a possible solution. Note that in that case no need for an id field, which is unused and just consumes space. You can delete the embeddable children by clearing the collection.
To improve performance you may need a different design in which not all the 500K are loaded each time together.