-->
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: Removing item from a List does not persist
PostPosted: Sat Feb 14, 2009 6:53 pm 
Newbie

Joined: Thu Feb 05, 2009 6:34 pm
Posts: 5
I am having trouble when removing an item from a list. The list is defined in a superclass, but the Hibernate annotations are applied to property accessors in a subclass. There are two methods in the superclass that manipulate the list. The "add" method works fine, but the "remove" does not persist changes. I have checked my Cascade settings, and I seem to have things correct. Am I doing something that is impossible. If not, am I doing something incorrectly?

I have two classes such as this:

Code:
@Entity
abstract class Temporal<T> {
    @Id
    @GeneratedValue
    private Long id;
   
    @Version
    private Integer version = null;

    @Transient
    protected List<T> content = new ArrayList<T>();

    public void remove(T value) {
        // business logic ...
        content.remove(value);
    }

    public void add(T value) {
        // business logic ...
        content.add(value);
    }
}

@Entity
@AccessType("property")
class TemporalAsset extends Temporal<Asset> {
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "temporal")
    public List<Asset> getContent() {
        return super.content;
    }

    protected void setContent(List<Asset> list) {
        super.content = list;
    }
}


I use an instance of the TemporalAsset class as follows:

Code:
temporalAsset.add(value1);
temporalAsset.getContent().size() == 1; // true
session. update(temporalAsset);

session.refresh(temporalAsset);

temporalAsset.getContent().size() == 1; // true

temporalAsset.remove(value1);
temporalAsset.getContent().size() == 0; // true
session.update(temporalAsset);

session.refresh(temporalAsset);

temporalAsset.getContent().size() == 0; // false, its 1


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2009 8:42 pm 
Newbie

Joined: Thu Feb 05, 2009 6:34 pm
Posts: 5
Note that I am only using "refresh" here to demonstrate the problem. I get the same behavior if i "flush" or close the session and open a new one. I have also verified that the version of the temporalAsset is incremented. So it has been saved, but the list still contains the value.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2009 6:14 am 
Newbie

Joined: Thu May 29, 2008 6:24 am
Posts: 5
have you tried following option?

@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})

I also faced similar issue. Above Cascade option solved my problem


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2009 11:39 am 
Newbie

Joined: Thu Feb 05, 2009 6:34 pm
Posts: 5
That was it! I had tried adding SAVE_UPDATE before, but I never thought to add DELETE_ORPHAN.

Thanks so much! I was really at wits end on this one.


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.