aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problem deleting value-type object from Collection in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem deleting value-type object from Collection in Hibernate" Watch "Problem deleting value-type object from Collection in Hibernate" New topic
Author

Problem deleting value-type object from Collection in Hibernate

kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
I'm using Hibernate. I have no problem deleting Entities (with ID value) but I seem to be having some trouble deleting regular Value type objects from a Collection. It seems to me that if you call remove(), the object should be removed. However, after doing this, the Object is removed from the Set, but when I load the View, the Object is again retrieved from the Set and displayed?!


[ August 15, 2007: Message edited by: kwame Iwegbue ]

Who dares, wins! (SAS motto)
Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
Hi Kwame,

If I understand your question correctly....

Removing an entity (Diagnoses) from the Collection does not remove it from the database. It's just removed from the Collection. The flush just get's whatever is in the database and those records still exist. You might want to call a hibernate session's remove on the Diagnoses objects to get the desired effect.


Shailesh Kini.
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Thanks Shailesh,

The thing is, the Diagnoses object is not an entity, but a Collection of value types without a database identity. They are embedded in another class, which is an Entity. When I remove one of the objects in the List, it is deleted from the List as expected, but when the List is called from the JSP, it is again loaded from the database. How can I overcome this? I can't call session.delete() because I dont want to delete the Entity.
[ August 16, 2007: Message edited by: kwame Iwegbue ]
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Can someone please help with this problem. Its been over a week, and I still can't figure it out. Let me rephrase the question:
Is there a special way of handling Collections of objects in Hibernate, when those objects do not have a database identity. I know that Objects with identiy, mapped in a Collection with *-to-many, are managed by the Hibernate session and can be removed by session.remove() etc; but how about regular objects?

When I try to remove them like from the Collection, they are reloaded from the database when the Collection is again accessed. Please help!!!
[ August 22, 2007: Message edited by: kwame Iwegbue ]
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
I see a contradiction here: how can you load from a database a collection of
objects that have no database identity?
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Here's whats going on:



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



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:



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!
[ August 23, 2007: Message edited by: kwame Iwegbue ]
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
What is visitDAO?
[ August 24, 2007: Message edited by: Edvins Reisons ]
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
visitDAO is a data access object that loads the Entity, Visit from the database.

Has no one out there using Hibernate, run into this type of situation that I describe in this question, or is it still unclear what I'm asking, or is it just a dumb question. Can some hibernate guru please help me with this!!
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
I asked the question about visitDAO because the persistence behavior of the
contained collection may depend on what is inside the DAO. Before digging into the DAO, let's make sure that the situation is as you describe: simple (non-collection) properties of the entity do get persisted to the database,
but the collection doesn't. Have you checked this?
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Thanks.
All other properties of the containing Entity (Visit) are managed correctly by hibernate, including other Collections, which are Collections of Entities mapped by one-to-many association. Even this particular troublesome Collection of Value type objects (Diagnoses) is managed correctly in every way, except when it comes to delete. According to everything I've read so far, removing a Value type object from a Collection should cause it to be deleted as well.
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Could it be that the Visit entity that you return from the DAO is detached?
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
detached huh?, Well I'm using the Open session in View pattern, i.e the whole request-response cycle is wrapped in a Hibernate Session. So I guess it could be detached after the delete method exits or something. But why would that be a problem. Do I need to re-attach it after the loop or what?
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
The problem with a detached entity is that whatever you do to it is not going to be persisted to the database. So, this is definitely worth checking, although I only half believe that this is what is going on here.
Please post the code of the DAO class(es) that get called so that we can see better what is happening.
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
I agree with you about detached objects, but my feeling was that with Open Session in View Pattern (which is what I'm using) , the persistence-context (unit of work) is wrapped by the request-responce cycle (in a servlet filter) so you application objects never get detached on their own, unless I close the persistence context prematurely. So I'm not sure this is the problem.

The Code for my VisitDAO is



the actual method is from GenericHibernateDAO, which is



The rest of the methods in the DAO are simply database queries.
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
What happens when you move the entity manipulation code into the DAO?
I think it belongs there anyway, and if the Session is, as it is in a
sample that I have here , a member variable of the DAO, doing so
would remove questions about visibility between the Session and the entity that you are modifying.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem deleting value-type object from Collection in Hibernate
 
Similar Threads
Sets in hibernate?
delete cascade problem
How to query a value typed object
@ManyToMany - delete inverse side object
need help with cascade delete