]> git.mxchange.org Git - jfinancials-ejb.git/commitdiff
Product-only:
authorRoland Häder <roland@mxchange.org>
Sat, 25 Apr 2020 16:25:40 +0000 (18:25 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 25 Apr 2020 16:28:04 +0000 (18:28 +0200)
- updating an entity doesn't work with a persist() invocation, not at least
  what JPA 2.1 says. As here JPA 2.1 is honored and not a specific
  implementation like Hibernate is, persist() will lead to a double entry.
  You need to use find() + copy all fields + merge() to archive this the proper
  way under JPA 2.1
- updated method invocations due to updated jproduct-core.jar

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jfinancials/enterprise/product/BaseFinancialsProductEnterpriseBean.java
src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java
src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java

index 2dab641b60973638579f9b0963e26023f8ed9d69..afd47f57b3fda0917cb5a840301f2d1be5195571 100644 (file)
@@ -19,6 +19,7 @@ package org.mxchange.jfinancials.enterprise.product;
 import java.text.MessageFormat;
 import java.util.Date;
 import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
+import org.mxchange.jproduct.model.category.Categories;
 import org.mxchange.jproduct.model.category.Category;
 import org.mxchange.jproduct.model.product.Product;
 import org.mxchange.jproduct.model.product.Products;
@@ -133,7 +134,7 @@ public abstract class BaseFinancialsProductEnterpriseBean extends BaseFinancials
         * <p>
         * @return Managed product instance
         */
-       protected Product mergeProductData (final Product detachedProduct) {
+       protected Product mergeGenericProductData (final Product detachedProduct) {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: detachedProduct={0} - CALLED!", detachedProduct)); //NOI18N
 
@@ -174,4 +175,52 @@ public abstract class BaseFinancialsProductEnterpriseBean extends BaseFinancials
                return managedProduct;
        }
 
+       /**
+        * Merges given category's data
+        * <p>
+        * @param detachedCategory Category instance to merge
+        * <p>
+        * @return Managed category instance
+        */
+       protected Category mergeProductCategoryData (final Category detachedCategory) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeCategoryData: detachedCategory={0} - CALLED!", detachedCategory)); //NOI18N
+
+               // The category instance must be valid
+               if (null == detachedCategory) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedCategory is null"); //NOI18N
+               } else if (detachedCategory.getCategoryId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedCategory.categoryId is null"); //NOI18N //NOI18N
+               } else if (detachedCategory.getCategoryId() < 1) {
+                       // Not valid
+                       throw new IllegalStateException(MessageFormat.format("detachedCategory.categoryId={0} is not valid.", detachedCategory.getCategoryId())); //NOI18N
+               }
+
+               // Set updated timestamp
+               detachedCategory.setCategoryEntryUpdated(new Date());
+
+               // Get category from it and find it
+               final Category foundCategory = this.getEntityManager().find(detachedCategory.getClass(), detachedCategory.getCategoryId());
+
+               // Should be found
+               assert (foundCategory instanceof Category) : MessageFormat.format("Category with id {0} not found, but should be.", detachedCategory.getCategoryId()); //NOI18N
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeCategoryData: foundCategory.categoryId={0}", foundCategory.getCategoryId())); //NOI18N
+
+               // Copy all
+               Categories.copyCategoryData(detachedCategory, foundCategory);
+
+               // Merge category instance
+               final Category managedCategory = this.getEntityManager().merge(foundCategory);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeCategoryData: managedCategory={0} - EXIT!", managedCategory)); //NOI18N
+
+               // Return detached category
+               return managedCategory;
+       }
+
 }
index 0b0fe75b43a18c727c9a4a5d817ee4ea11528eb5..db6e5071b07a50641cc22b6f03f58960c0f4bdb7 100644 (file)
@@ -85,7 +85,7 @@ public class FinancialsAdminProductCategorySessionBean extends BaseFinancialsPro
                }
 
                // Set created instance
-               category.setCategoryCreated(new Date());
+               category.setCategoryEntryCreated(new Date());
 
                // Persist it
                this.getEntityManager().persist(category);
@@ -129,17 +129,14 @@ public class FinancialsAdminProductCategorySessionBean extends BaseFinancialsPro
                        category.setParentCategory(managedCategory);
                }
 
-               // Set updated instance
-               category.setCategoryUpdated(new Date());
-
-               // Persist it
-               this.getEntityManager().persist(category);
+               // Update instance
+               final Category updatedCategory = this.mergeProductCategoryData(category);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductCategory: category.categoryId={1} - EXIT!", this.getClass().getSimpleName(), category.getCategoryId())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductCategory: updatedCategory.categoryId={1} - EXIT!", this.getClass().getSimpleName(), updatedCategory.getCategoryId())); //NOI18N
 
                // Return it
-               return category;
+               return updatedCategory;
        }
 
        /**
index 4cd08166e5fd02bccec82269994aa6d5008b8f13..26aa418bc6c40d98f43661efb0971ef938bff546 100644 (file)
@@ -134,7 +134,7 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
                }
 
                // Merge data
-               final Product managedProduct = this.mergeProductData(product);
+               final Product managedProduct = this.mergeGenericProductData(product);
 
                // Set updated instance
                managedProduct.setProductEntryUpdated(new Date());