This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem deleting value-type object from Collection in Hibernate

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is visitDAO?
[ August 24, 2007: Message edited by: Edvins Reisons ]
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be that the Visit entity that you return from the DAO is detached?
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Whatever you say buddy! And I believe this tiny ad too:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic