Forums Register Login

Adding/Deleting children from collection w/o fetching the whole collection

+Pie Number of slices to send: Send
Hi, I'm new to annotations & using Hibernate outside of Spring. I'm not sure what my configuration needs to be in order to do this (or if you can)

I'm retrieveing a child entity directly from Hibernate with a query including it's parent entity with cross-references. The child entity is the only object in a collection which could have another (1000?) rows in the db with same foreign key value.

I process some logic to create 2 or more instances of the child class and distribute some numeric values between them. The I want the original row deleted and the new entities persisted.



it seems that as soon as I hit the first add, that the entire collection is fetched. Is there a way to supress this and still have it work correctly? There is an obvious overhead in create 1000+ objects that are not needed.

Here are my mappings:

Parent:
@OneToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY, mappedBy = "payrollHeader")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Set<PayrollDetail> payrollDetails = new HashSet<PayrollDetail>();
...
Child:
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PayrollHeaderID", nullable=false)
public PayrollHeader getPayrollHeader() {
return this.payrollHeader;
}

Thanks for any insight.
+Pie Number of slices to send: Send
Duh, obviously I can do explicit saves on the new children in a loop and explicit delete on the one I want to remove. If the parent controls everything does it need the whole collection fetched?

Thanks,
+Pie Number of slices to send: Send
Hi Tony Crisera,

your collection is mapped inverse so both ends "know" of the collection. There should be no problem if you save (or delete) your children without touching the parent - as long as you have a reference to the parent in your child instance. You should also be able to see this from your DB schema. I'd expect that your child table has a FK column referencing the parent table whereas the parent table does not show any sign of "knowledge" about the child table.

On your original issue with the collection fetching. This might be dependent on the kind of collection you choose. I could imagine a HashSet has to be loaded completely in order to decide if the child is actually to be added or not. For each existing entry the hashCode must be calculated to check if the new item is actually a duplicate (will not add) or not (will add).

CU
Froestel
+Pie Number of slices to send: Send
Thanks, I kinda figured that when I thought about it a while. Went ahead and did just that.
Thanks again.
Your buns are mine! But you can have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1037 times.
Similar Threads
2nd Level cache eviction not cascading
Why does FetchType influence number of results!?!
Can't remove (delete) rows in one to many relationship
Improving An Algorithm
hibernate search problem w/ entity inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:10:54.