[Lada-commits] [PATCH] Push down refreshing of persisted objects deeper into the stack

Wald Commits scm-commit at wald.intevation.org
Wed Feb 8 18:03:12 CET 2017


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1486573325 -3600
# Node ID 14876c62f6920e336ef22eb77fb8a7b709250edc
# Parent  788311087838e824f0f8cf08560b21ef25388edf
Push down refreshing of persisted objects deeper into the stack.

There are more places besides creation of Probe objects where it is
useful to return within the response what has been really written to
the database (including modifications by the database itself) instead
of merely the request data, e.g. creation of Ort objects, which
includes database generated ort_ids.

diff -r 788311087838 -r 14876c62f692 src/main/java/de/intevation/lada/rest/ProbeService.java
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Feb 08 17:46:45 2017 +0100
+++ b/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Feb 08 18:02:05 2017 +0100
@@ -313,21 +313,16 @@
             probe = factory.findUmweltId(probe);
         }
         probe = factory.findMediaDesk(probe);
+
         /* Persist the new probe object*/
         Response newProbe = repository.create(probe, "land");
-        Probe ret = (Probe)newProbe.getData();
-        // Refreshing the probe object is necessary because probe objects use
-        // dynamic-insert, meaning null values are not written to the db and not
-        // updated after insert.
-        Response refreshed =
-            repository.getById(Probe.class, ret.getId(), "land");
-        /* Create and persist a new probe translation object*/
+
         if(violation.hasWarnings()) {
-            refreshed.setWarnings(violation.getWarnings());
+            newProbe.setWarnings(violation.getWarnings());
         }
         return authorization.filter(
             request,
-            refreshed,
+            newProbe,
             Probe.class);
     }
 
diff -r 788311087838 -r 14876c62f692 src/main/java/de/intevation/lada/util/data/DataTransaction.java
--- a/src/main/java/de/intevation/lada/util/data/DataTransaction.java	Wed Feb 08 17:46:45 2017 +0100
+++ b/src/main/java/de/intevation/lada/util/data/DataTransaction.java	Wed Feb 08 18:02:05 2017 +0100
@@ -48,7 +48,14 @@
         EJBTransactionRolledbackException,
         TransactionRequiredException
     {
-        emp.entityManager(dataSource).persist(object);
+        EntityManager manager = emp.entityManager(dataSource);
+        manager.persist(object);
+
+        /* Refreshing the object is necessary because some objects use
+           dynamic-insert, meaning null-valued columns are not INSERTed
+           to the DB to take advantage of DB DEFAULT values, or triggers
+           modify the object during INSERT. */
+        manager.refresh(object);
     }
 
     /**
diff -r 788311087838 -r 14876c62f692 src/main/java/de/intevation/lada/util/data/DefaultRepository.java
--- a/src/main/java/de/intevation/lada/util/data/DefaultRepository.java	Wed Feb 08 17:46:45 2017 +0100
+++ b/src/main/java/de/intevation/lada/util/data/DefaultRepository.java	Wed Feb 08 18:02:05 2017 +0100
@@ -44,7 +44,8 @@
      * @param object The new object.
      * @param dataSource The datasource.
      *
-     * @return Response object containing the new object.
+     * @return Response object containing the new object, potentially
+     *         modified by the database.
      */
     @Override
     public Response create(Object object, String dataSource) {


More information about the Lada-commits mailing list