changeset 1290:14876c62f692

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.
author Tom Gottfried <tom@intevation.de>
date Wed, 08 Feb 2017 18:02:05 +0100
parents 788311087838
children d48e1636fb0b
files src/main/java/de/intevation/lada/rest/ProbeService.java src/main/java/de/intevation/lada/util/data/DataTransaction.java src/main/java/de/intevation/lada/util/data/DefaultRepository.java
diffstat 3 files changed, 14 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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);
     }
 
--- 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);
     }
 
     /**
--- 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) {
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)