-->
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: Deleting objects from a collection
PostPosted: Wed Feb 15, 2012 1:03 am 
Newbie

Joined: Wed Feb 15, 2012 12:11 am
Posts: 2
I would like to ask for your help on a hibernate question.

We have a system where we load an object from our relational database and serialize it into JSON.
Our Class looks like this:
Code:
@Entity
@Table(name = "mytable")
public class MyClass implements Serializable {
    @Id
    @NotNull
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id = 0L;

    @OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name = "parent_id", updatable = false)
    private List<ChildObject>  collection = new ArrayList<ChildObject>();
}

The object serialized into json might look a little like:

Code:
{
    "id" :"12",
    "collection": [
        {
            "id": 1,
            "parent_id": "12"
        },
        {
            "id": 2,
            "parent_id": "12"
        }
    ]
}


We modify the json in the client, and then send it back to the server. The json gets deserialized back into an object, and the database is updated via hibernate.
Code:
MyClass myObject = new JSONDeserializer< MyClass >().deserializeInto(json, new  MyClass());

sessionFactory.getCurrentSession().update(myObject);


This works fine when updating properties, and adding objects to the collection. My only problem comes when I want to remove an object from the collection, and have it deleted from my database. I have looked for the solution to this everywhere and have tried many variations on Cascade annotations and the deleteOrphan = true annotation without success.

I recieve the following JSON from the client, and notice that the client has removed the first element in the collection:
Code:
{
    "id" :"12",
    "collection": [
        {
            "id": 2,
            "parent_id": "12"
        }
    ]
}


I deserialize the json back into my object and update via hibernate. The missing element is ignored, and remains in the database.
Code:
MyClass myObject = new JSONDeserializer< MyClass >().deserializeInto(json, new  MyClass());

sessionFactory.getCurrentSession().update(myObject);


Perhaps I am going about this the wrong way, can someone please help me out.

Many thanks,

Dan


Top
 Profile  
 
 Post subject: Re: Deleting objects from a collection
PostPosted: Mon Feb 20, 2012 8:18 pm 
Newbie

Joined: Wed Feb 15, 2012 12:11 am
Posts: 2
If anyone else was stuck on this, it turns out the issue was not the hibernate annotations, but the method for persisting the data.

With the following annotation:
Code:
    @OneToMany(fetch=FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "parent_id", updatable = false)
    private List<ChildObject>  collection = new ArrayList<ChildObject>();


I changed the following line:
Code:
sessionFactory.getCurrentSession().update(myObject);

to
Code:
sessionFactory.getCurrentSession().merge(myObject);


And it worked fine.

Here is a post by someone with the same issue and a discussion on the difference between update() and merge()
viewtopic.php?f=1&t=987543


Top
 Profile  
 
 Post subject: Re: Deleting objects from a collection
PostPosted: Mon Feb 20, 2012 11:20 pm 
Newbie

Joined: Fri Feb 17, 2012 8:20 am
Posts: 18
Yeah i too agree....

Difference Between Merge And Update Methods In Hibernate


Top
 Profile  
 
 Post subject: Re: Deleting objects from a collection
PostPosted: Tue Feb 21, 2012 1:04 am 
Newbie

Joined: Tue Feb 21, 2012 12:57 am
Posts: 5
I also agree.

_________________
nostale gold


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.