-->
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: Hibernate does not delete Objects
PostPosted: Wed Feb 03, 2010 11:49 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello,
I am working on an Graphical Editor based on GEF. The Model is saved in a database using hibernate.
All of my model-objects are fetched eager.
I have 2 EntityManagers, one to read model data and the second to write changes back to database via merge
First reading example:
Code:
static public Diagram findByProjectAndName(String pProjectName, String pName) {
      Diagram lResult = null;
      sReadingManager.clear();
      sReadingManager.getTransaction().begin();
      List<Diagram> lDiagrams = sReadingManager.createQuery(
            "from Diagram d where d.project.name = '" + pProjectName + "' " +
            "and d.name = '" + pName + "'")
            .getResultList();
      if (lDiagrams.size() == 1) {
         lResult = lDiagrams.get(0);
      }
      sReadingManager.getTransaction().commit();
      return lResult;
   }


Second, write data back to db:

Code:
   static public void mergeDiagram(Diagram pDiagram) {
      sWritingManager.getTransaction().begin();
      Diagram lDia = sWritingManager.merge(pDiagram);
      sWritingManager.getTransaction().commit();
      
   }


Everything works fine so far. I get the diagram, do some changes to its model and then clall merge after saving.
But there is still one Problem left, if I delete an element of the diagram, hibernate will not delete it in the database. Hibernate will only set the references on the deleted element to null:
DEBUG SQL:393 - update DiagramElement set name=?, viewInfo=?, diagram_fk=? where DIAGRAMELEMENT_ID=?
DEBUG SQL:393 - update DiagramElement set diagram=null where diagram=?

Do I have to delete the element explicit in a query or am I doing something wrong?

I would appreciate any help!

Thanks in advance & best regards.


Top
 Profile  
 
 Post subject: Re: Hibernate does not delete Objects
PostPosted: Wed Feb 03, 2010 6:13 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Are you just making the object null, or calling delete? Making it null won't delete it. Nullifying it will likely just delete an association with an aggregating object.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Hibernate does not delete Objects
PostPosted: Thu Feb 04, 2010 2:47 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello and first of all thanks for your reply!
How should I delete it?
At the moment I am removing the Element from the List of DiagramElements in my Diagram via list.remove(diagramElement)
So what else should I do to delete the object in database, too.
Please be aware that I am working with detached objects and all my changes on the model are updated at the end.

Thanks for your help.
Best regards
Alex


Top
 Profile  
 
 Post subject: Re: Hibernate does not delete Objects
PostPosted: Thu Feb 04, 2010 6:55 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello again,
I figured out that JPA causes the problem with deleting detached objects.
During the long unit of work, the user can change the model outside of a transaction (detached objects!), so if the user deleted an object it is not possible to call lEntitityManager.remove(modelObject)
So I need something which automatically removes all orphans from the database. In hibernate an annotiation CascadeType.DELETE_ORPHANS exists, but in JPA there is no annotation like this supported.
Perhaps there is an easy way to get the same functionality with JPA and if so I would appreciate any example :)
But there is still another problem left:
Calling sWritingManager.merge(pDiagram) causes to reload all instances.
For example before merging, DiagramElement A has the object_id 1 and after merging it has the object_id 2.
But not all instances in the application are reloaded. If I had any references to DiagramElement A, the reference is now out of date (because the reference is on DiagramElement A with object_id 1).
Any opportunities to solve this?
Thanks in advance & best regard,
Alex


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.