-->
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.  [ 7 posts ] 
Author Message
 Post subject: Trying to get a deleted object --> ObjectDeletedException
PostPosted: Mon Apr 11, 2005 5:30 am 
Beginner
Beginner

Joined: Wed Mar 30, 2005 5:41 am
Posts: 40
Hi, I am doing some tests (see the code below), I instantiate a new Worker object, set some properties, save it, delete it and after the deletion try to retrieve it from the database.

I was expecting a null pointer returned but get a ObjectDeletedException.

The first time, I was using the load method (session) but I red I should use this method only for objects that I am sure they exist, so I replace the load method by the get method but the result is the same...

Thank you
Best regards
Lilian


Hibernate version:
3.0

JUnit test method:
Code:
public void testSaveOneWorker()
   {   
      Worker john = new Worker();
      
      assertNull( john.getId() );
      
      john.setAffiliationDate( new Date() );
      john.setWorkPermitCode( "worker.permit.ch" );
      
      dao.save( john );
      
      assertNotNull( john.getId() );
      
      Integer johnId = john.getId();
      
      dao.delete( john );
      
      john = dao.getWorkerById( johnId, false );
      
      assertNull( john );
   }

the getWorkerById method:
Code:
Session session = HibernateUtil.currentSession();
      Worker worker = null;
      try
      {
         if ( lock )
         {
            worker = (Worker)session.get( Worker.class, workerId, LockMode.UPGRADE );
         }
         else
         {
            worker = (Worker)session.get( Worker.class, workerId );
         }
      }
      catch ( HibernateException ex )
      {
         throw new InfrastructureException( ex );
      }
      return worker;

Full stack trace of any exception that occurs:
11:23:19,398 INFO DefaultLoadEventListener:129 - Error performing load command
org.hibernate.ObjectDeletedException: The object with that id was deleted: [bab.admin.model.persistent.Worker#10]
at org.hibernate.event.def.DefaultLoadEventListener.loadFromSessionCache(DefaultLoadEventListener.java:430)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:166)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:210)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:123)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:561)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:556)
at bab.admin.model.dao.WorkersDAO.getWorkerById(WorkersDAO.java:49)
at bab.admin.model.dao.WorkersDAOTests.testSaveOneWorker(WorkersDAOTests.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)


Name and version of the database you are using:
MySQL 4.1

The generated SQL (show_sql=true):
Hibernate: insert into t_party (comment, partyType) values (?, 'worker')
Hibernate: insert into t_avs (cotiseYN, avsNumber, requestDate, expeditionDate, receptionDate, partyId) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into t_person (sex, title, maritalStatusCode, birthdate, accountInfo, firstname, lastname, partyId) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into t_worker (enabledYN, critic, compliment, motivationCode, motivationShortCode, affiliationDate, lastReactivationDate, workPermitCode, authorizationExpirationDate, sourceYN, activityId, partyId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into t_workerDisponibility (mondayMorningYN, mondayAfternoonYN, mondayNightYN, tuesdayMorningYN, tuesdayAfternoonYN, tuesdayNightYN, wednesdayMorningYN, wednesdayAfternoonYN, wednesdayNightYN, thursdayMorningYN, thursdayAfternoonYN, thursdayNightYN, fridayMorningYN, fridayAfternoonYN, fridayNightYN, saturdayMorningYN, saturdayAfternoonYN, saturdayNightYN, sundayMorningYN, sundayAfternoonYN, sundayNightYN, februaryYN, paquesYN, summerYN, autumnYN, noelYN, jeunegenevoisYN, ascensionYN, pentecoteYN, partyId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update t_worker set enabledYN=?, critic=?, compliment=?, motivationCode=?, motivationShortCode=?, affiliationDate=?, lastReactivationDate=?, workPermitCode=?, authorizationExpirationDate=?, sourceYN=?, activityId=? where partyId=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 6:17 am 
Newbie

Joined: Thu Feb 03, 2005 5:49 am
Posts: 19
I was in that situation and I realized if I did flush the session I didn't get the ObjectDeletedException.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 6:21 am 
Beginner
Beginner

Joined: Wed Mar 30, 2005 5:41 am
Posts: 40
sambuko wrote:
I was in that situation and I realized if I did flush the session I didn't get the ObjectDeletedException.


As you can imagine, I worked around this problem and I dont remember how ;)

Thank you for your message.
Lilian


Top
 Profile  
 
 Post subject: Good question - perhaps someone has an explanation?
PostPosted: Mon Oct 10, 2005 11:17 am 
Newbie

Joined: Fri Jul 08, 2005 5:00 am
Posts: 2
sambuko wrote:
Quote:
I was in that situation and I realized if I did flush the session I didn't get the ObjectDeletedException.


Yes, because flushing the session clears all references from the session to the deleted object. The question is: Why does this behaviour occur at all? For me, it seems perfectly reasonable to refer to the deleted object within the same session via get() in order to be sure the object is really deleted (as is a default approach in a test case). I understand that there are cases in which throwing ObjectDeletedException is the right way - for example, if an attempt is made to update the deleted object. But for get()?

This issue is recurring every now and then, see for example my posting here:
http://forum.hibernate.org/viewtopic.php?t=944819
As far as I can see, the issue has never been really answered in this forum.

Perhaps someone has an explanation?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 16, 2006 12:57 pm 
Newbie

Joined: Thu Dec 08, 2005 3:54 pm
Posts: 11
We're having the exact same issue. I don't see where it's ever been answered.

In our JUnit test, we:
    create a domain object
    record its id
    delete the domain object
    try to find the deleted object by calling a DAO's findById method

We expected to get a null pointer returned since the object was just deleted. Instead, an ObjectDeletedException is thrown our way.

This is particularly troublesome because our test designer believes his test to be a fair test (after all - it works with our other, non-Hibernate DAOs). So now the team wants to redesign our Hibernate DAO findById methods. They want to silently catch ObjectDeletedException and return null values instead of throwing this internal Hibernate exception. I think this clutters up/complicates our findById implementation - but I don't see a way around it.

I'd like to leave our findById method as simple as this one line of code - but we can't if Hibernate is sometimes going to throw an exception for some IDs:

Code:
return (MyClass) HibernateUtil.getSession().get(MyClass.class, new Integer(targetId));


Obviously - the question remains - why does Hibernate throw the exception instead of returning null for findById? If a user calls a findById - why does it matter if the ID recently existed vs. the ID never existed?

Notice that we're using the "get" method on Session, not the "load" method. The javadoc documenation for "get" reads:

Quote:
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)


While the javadoc documentation for "load" reads:

Quote:
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.

You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.


So - by my reading of this documentation - calling "load" could very well throw an ObjectDeletedException for certain IDs, by why does "get" throw the exception? I think that - from the documentation - "get" should return null, since "get" is supposed to be used for checking to see if an object exists (or - by extension - if an object still exists). An existence check shouldn't throw an Exception for an object that doesn't exist.

I've seen multiple posts on this topic but no answers (and no references in the FAQs or documentation that I can find).

Help?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 16, 2006 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
http://opensource.atlassian.com/project ... e/HHH-1818

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 11:59 am 
Newbie

Joined: Tue Oct 03, 2006 2:07 am
Posts: 18
In 3.2.5.ga it isnĀ“t working again (or still) :-(


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