details:   https://code.openbravo.com/erp/devel/pi/rev/72cd92b69d62
changeset: 30703:72cd92b69d62
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Thu Dec 01 16:10:42 2016 +0100
summary:   fixed bug 34657: inconsistent behavior of OBDal.get

  OBDal.get behaved inconsistently when trying to obtain an object for an ID 
that
  is not present in DB:

   -It usually returns null
   -unless there was a proxy for the same object, which thrown an exception

  Fixed by making it to always return null in this case.

diffstat:

 src-test/src/org/openbravo/test/dal/DalTest.java |  35 ++++++++++++++++++++++++
 src/org/openbravo/dal/service/OBDal.java         |  10 ++++++-
 2 files changed, 44 insertions(+), 1 deletions(-)

diffs (102 lines):

diff -r e9f96baecaae -r 72cd92b69d62 
src-test/src/org/openbravo/test/dal/DalTest.java
--- a/src-test/src/org/openbravo/test/dal/DalTest.java  Thu Dec 01 16:03:13 
2016 +0100
+++ b/src-test/src/org/openbravo/test/dal/DalTest.java  Thu Dec 01 16:10:42 
2016 +0100
@@ -22,17 +22,23 @@
 
 package org.openbravo.test.dal;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.util.List;
 
 import org.apache.log4j.Logger;
+import org.hibernate.ObjectNotFoundException;
 import org.hibernate.criterion.Restrictions;
 import org.junit.FixMethodOrder;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.junit.runners.MethodSorters;
 import org.openbravo.base.exception.OBSecurityException;
 import org.openbravo.base.model.Property;
@@ -44,6 +50,7 @@
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.system.SystemInformation;
+import org.openbravo.model.common.businesspartner.BusinessPartner;
 import org.openbravo.model.common.businesspartner.Category;
 import org.openbravo.model.common.businesspartner.CategoryAccounts;
 import org.openbravo.model.common.currency.Currency;
@@ -65,6 +72,9 @@
 public class DalTest extends OBBaseTest {
   private static final Logger log = Logger.getLogger(DalTest.class);
 
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   /**
    * Test to assert save false in a null char(1) column - Part I
    */
@@ -394,4 +404,29 @@
     final Property property = DalUtil.getProperty("AD_COLUMN", "AD_COLUMN_ID");
     assertNotNull(property);
   }
+
+  @Test
+  public void getInexistentObjShouldBeNull() {
+    BusinessPartner bp = OBDal.getInstance().get(BusinessPartner.class, 
"DummyId");
+
+    assertThat(bp, is(nullValue()));
+  }
+
+  @Test
+  public void getInexistentObjShouldBeNullEvenIfItWasProxied() {
+    @SuppressWarnings("unused")
+    BusinessPartner bpProxy = 
OBDal.getInstance().getProxy(BusinessPartner.class, "DummyId");
+    BusinessPartner bp = OBDal.getInstance().get(BusinessPartner.class, 
"DummyId");
+
+    assertThat(bp, is(nullValue()));
+  }
+
+  @Test
+  public void populatingProxyOfInexistentObjShouldFail() {
+    BusinessPartner bpProxy = 
OBDal.getInstance().getProxy(BusinessPartner.class, "DummyId");
+
+    thrown.expect(ObjectNotFoundException.class);
+    // getting any property causes proxy population from db
+    bpProxy.getName();
+  }
 }
diff -r e9f96baecaae -r 72cd92b69d62 src/org/openbravo/dal/service/OBDal.java
--- a/src/org/openbravo/dal/service/OBDal.java  Thu Dec 01 16:03:13 2016 +0100
+++ b/src/org/openbravo/dal/service/OBDal.java  Thu Dec 01 16:10:42 2016 +0100
@@ -25,6 +25,7 @@
 import java.util.List;
 
 import org.apache.log4j.Logger;
+import org.hibernate.ObjectNotFoundException;
 import org.hibernate.Session;
 import org.hibernate.criterion.Restrictions;
 import org.hibernate.dialect.Dialect;
@@ -295,7 +296,14 @@
    */
   public <T extends Object> T get(Class<T> clazz, Object id) {
     checkReadAccess(clazz);
-    return SessionHandler.getInstance().find(clazz, id);
+    try {
+      return SessionHandler.getInstance().find(clazz, id);
+    } catch (ObjectNotFoundException ignore) {
+      // ObjectNotFoundException is thrown when there was a proxy in cache for 
this id but the
+      // record does not exist in DB. As if there was no proxy, the same 
invokation would return
+      // null, let's be consistent and return null also in this case.
+      return null;
+    }
   }
 
   /**

------------------------------------------------------------------------------
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to