• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tony Crisera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Crisera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Andreas Erber
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Crisera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I kinda figured that when I thought about it a while. Went ahead and did just that.
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic