-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Cascade behaviour
PostPosted: Tue Feb 03, 2009 11:30 am 
Newbie

Joined: Tue Feb 03, 2009 11:23 am
Posts: 4
HI, I'm trying to understand the behaviour of cascade

This is my mapping for a 'File' object, which can have many 'Field' objects

Code:
    @OneToMany(fetch=FetchType.LAZY)
    @JoinTable(
            name="BULK_UPLOAD_FILE_TYPE_FIELD",
            joinColumns = @JoinColumn(name="BULK_UPLOAD_FILE_TYPE_ID", referencedColumnName="BULK_UPLOAD_FILE_TYPE_ID"),
            inverseJoinColumns = @JoinColumn(name="BULK_UPLOAD_FILE_FIELD_ID", referencedColumnName="BULK_UPLOAD_FILE_FIELD_ID")
    )
    private List<BulkUploadFileField> fields = new ArrayList<BulkUploadFileField>();



Now, the File object does have a collection of Fields in the database, but in my code, I just want to save the details of the File object in one case (when the user has modified some values at the front end)

So from the front end, I pass in parameters and reconstruct the File parent object MINUS the Fields that are associated with it (File.getFields() would return null)

Since I have no cascade settings on my OneToMany, I was expecting the 'merge' operation to persist the new values of File object, but leave the collection alone.

However, it deletes all the Field associations in my DB join table (I can see the delete statements that Hibernate generates)

I only want Hibernate to delete/update etc my Field associations when I explicitly tell it - what am I doing wrong?

Thanks!

PS I can get around this by loading a fresh copy of the File object from the database (complete with it's populated Field collection) and then overwriting the parent's values with what the user has supplied, but the extra read operation seems inefficient


Last edited by gunderr on Tue Feb 03, 2009 11:37 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 11:37 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You should map the association bidirectionally and make the other side to the owner of the association (using mappedBy). In your case the File-object is the owner of the association, and as the collection is empty or null, it does the right thing.

Another solution instead of reconstructing it, would be to load the object from the database (the collection lazily), change it and then merge it.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 12:54 pm 
Newbie

Joined: Tue Feb 03, 2009 11:23 am
Posts: 4
Thanks mmerder, I used the second option and that works nicely


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 4:15 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You are welcome. Please rate me, if it helped.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.