cdpwest / hibernate-generic-dao

Automatically exported from code.google.com/p/hibernate-generic-dao
0 stars 0 forks source link

deleteById( id ) throws null pointer if the id does not exist #15

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I think the correct behavior would be to do nothing or return false.  (same
as if you pass in a null id)

Here is a patch that makes the change for: HibernateDAOHQLImpl.java

Index: src/main/java/com/trg/dao/hibernate/HibernateDAOHQLImpl.java
===================================================================
--- src/main/java/com/trg/dao/hibernate/HibernateDAOHQLImpl.java       
(revision 110)
+++ src/main/java/com/trg/dao/hibernate/HibernateDAOHQLImpl.java       
(working copy)
@@ -76,10 +76,15 @@
         * Delete the object of the specified class with the specified id
from the
         * database.
         */
-       protected void _deleteById(Serializable id, Class klass) {
-               if (id == null)
-                       return;
-               getSession().delete(getSession().get(klass, id));
+       protected boolean _deleteById(Serializable id, Class klass) {
+               if( id != null ) {
+               Object v = getSession().get(klass, id);
+               if( v != null ) {
+                 getSession().delete( v );
+                 return true;
+               }
+               }
+               return false;
        }

        /**

Original issue reported on code.google.com by ryan...@gmail.com on 21 Nov 2008 at 11:50

GoogleCodeExporter commented 8 years ago
here is a real patch also adds a simple test

Original comment by ryan...@gmail.com on 21 Nov 2008 at 11:53

Attachments:

GoogleCodeExporter commented 8 years ago

Original comment by dwolvert on 25 Nov 2008 at 7:39

GoogleCodeExporter commented 8 years ago
This fix will be in 0.3.3.

Thanks ryantxu.

Original comment by dwolvert on 25 Nov 2008 at 7:55