-->
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.  [ 5 posts ] 
Author Message
 Post subject: Unable to delete Value type from Collection
PostPosted: Mon Aug 27, 2007 9:39 am 
Newbie

Joined: Sat May 12, 2007 4:21 pm
Posts: 4
I am new to Hibernate, but I have read the tutorial and The 'King/Bauer' referrence book. I have an application built on Hibernate and it mostly works very well. I have no trouble managing Entity objects and associations of entities; I'm however having some difficulty managing a Collection of Value type objects. When I attempt to delete a Value type object form a Set that is mapped to an Entity, the object is initially removed from the Collection in the Controller, but when it returns to the View and the Entity is loaded from the database, it still contains the deleted object. I'm not sure what I could be doing wrong. I'm using Open Session in View pattern, so the request/response cycle is wrapped in a Filter.

Here is some code from the Entity object in my application:

Code:
 
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "VISIT")
public class Visit implements Serializable {

   
   @Id @GeneratedValue
   @Column(name="VISIT_ID")
   private Long id;

...

   @org.hibernate.annotations.CollectionOfElements
   @JoinTable(name = "DIAGNOSES", joinColumns = @JoinColumn(name = "VISIT_ID"))
   private Set<Diagnosis> diagnoses = new HashSet<Diagnosis>();

...

 


So Visit is an Entity (has database identity) and Diagnosis is a Value type (has no database identity).

I can manage a Visit object with

Code:
 
            getSession().saveOrUpdate(visit); 
            getSession().delete(visit);
           



However, this won't work with Diagnosis. So if I want to delete a Diagnosis object, which is in a Set that is mapped to a Visit object, I do this:

Code:
        
...
Visit v = visitDAO.findById(getVisit_id(), true);
Set<Diagnosis> diagnoses = v.getDiagnoses();
      
      for (Iterator iter = diagnoses.iterator(); iter.hasNext();    )   {
         Diagnosis element = (Diagnosis) iter.next();
         v.getDiagnoses().remove(element);
      }
...


So this works, and removes the Diagnosis from the Collection, however, when Visit is loaded again, the "deleted" Diagnosis is still present in the Collection!

Someone please help!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 12:58 pm 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
I think there exists at least three possible error causes (I'm a newbie too, so there might be lots of other):
-> You could have forgotten to call saveOrUpdate
-> You might be working on a copy of the set (ok, you should have recognized this if it were the case)
-> The relation between Diagnosis and Visit is bidirectional und you have forgotten to delete the other (leading) side of the relation: diagnosis.setVisit(null);

Regards
Hoeft


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 2:23 pm 
Newbie

Joined: Sat May 12, 2007 4:21 pm
Posts: 4
Thanks for the reply Hoeft,

according to the referrence textbook (Java persistence with Hibernate),
which says on page 529- and I quote:

Quote:
"any value-typed property (or component) value of an entity instance is deleted when the owning instance is deleted. Value-typed collection elements (for example, the collection of Image objects from Item) are deleted if you remove the referrences from the owning collection."


...So, My Visit object is an Entity which contains a Set of Diagnosis. I expected that simply calling remove should be enough to delete one of the simple Value-typed objects in this Collection. I expected that unlike with Entity objects, I wouldn'nt need to call saveOrUpdate or manage both sides of the association.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 2:21 pm 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
You're right, I didn't notice that you are talking about value types.

Are your sure that saveOrUpdate is called on the visit object after the diagnosis objects have been deleted? I think a missing saveOrUpdate call can be the only reason for your problem.

Hoeft


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 3:27 pm 
Newbie

Joined: Sat May 12, 2007 4:21 pm
Posts: 4
I call saveOrUpdate, and its still not working!!!!!!
Don't know what else to do!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.