-->
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: Help with detached object merge
PostPosted: Tue Aug 21, 2007 12:08 pm 
Newbie

Joined: Fri May 18, 2007 12:29 pm
Posts: 7
Hibernate version: 3.2.4

I'm working on a solution which pulls data from an foreign XML source into objects which are then persisted in the database.

The XML is simple:

Code:

<data>

   <widget id="A"/>
   <widget id="B"/>
   <widget id="C"/>

</data>



OK, so this XML maps to a Data object which contains a Set of Widget objects. Since these are detached objects, I call merge() to save/update them in my database.

Code:
// JAXB mappings here as well
@Entity
public class Data implements Serializable {
     
    // JAXB mappings here as well
    @OneToMany( cascade = {CascadeType.ALL} )     
    private Set<Widget> widgets = new HashSet<Widget>();     

    [...]
}



Merge:

Code:
     
       Data dataJAXB = (Data) getUnmarshaller().unmarshal(
                new File( path )
                );
               
        DataDAO dao = getFactory().getDataDAO();
       
        Data dataHIB = dao.findById( dataJAXB.getId(), true );

        dao.merge( dataJAXB );
       
        [...]



My problem is that I may not receive every widget tag but that doesn't mean it has been deleted.

I understand that I'm merging the current state in the XML file but is there a way I can specify the Cascade to always Save/Update existing but never remove a missing (in the XML file) association? I'll have logic further down the line which handles widgets which actually need to be disabled.

Any insight is greatly appreciated,
BG4


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 1:43 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
This has nothing to do with cascade so there's no way to configure it away. Cascade affects what happens to the widgets themselves when some action is performed on the parent, it doesn't have any bearing on the relationship between Data and its Widgets.

I think you'll have to merge the widgets from your XML obj into your managed obj manually.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 2:06 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I've had another thought about this...

If you make the Widget the controlling end of the relationship then you _will_ be able to merge the incomplete Data without deleting the missing widgets (i.e. what you wanted).

To make this work you need to set the parent Data into each widget and set the parent to null when you want to remove it from Data.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 3:00 pm 
Newbie

Joined: Fri May 18, 2007 12:29 pm
Posts: 7
thatmikewilliams wrote:
I've had another thought about this...

If you make the Widget the controlling end of the relationship then you _will_ be able to merge the incomplete Data without deleting the missing widgets (i.e. what you wanted).

To make this work you need to set the parent Data into each widget and set the parent to null when you want to remove it from Data.


Thank you for the response - this would work great as the widget is the primary focus anyway.


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.