]> git.mxchange.org Git - pizzaservice-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>
Wed, 10 Jun 2020 17:19:53 +0000 (19:19 +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/jproduct/model/category/PizzaAdminProductCategorySessionBean.java
src/java/org/mxchange/jproduct/model/product/PizzaAdminGenericProductSessionBean.java
src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java

index f85125bbca80c6fe2f88f70c2d16a47c3e264205..b8a34a1d78a7a32d4fcbf015ad273c15de34064f 100644 (file)
@@ -85,7 +85,7 @@ public class PizzaAdminProductCategorySessionBean extends BasePizzaProductEnterp
                }
 
                // Set created instance
-               category.setCategoryCreated(new Date());
+               category.setCategoryEntryCreated(new Date());
 
                // Persist it
                this.getEntityManager().persist(category);
@@ -129,17 +129,14 @@ public class PizzaAdminProductCategorySessionBean extends BasePizzaProductEnterp
                        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 3bc68cefbffd948a39c4753916a837d8ce5abc60..6e8867910f9b04b0e51d28a3c06ad2b2988a73a1 100644 (file)
@@ -134,7 +134,7 @@ public class PizzaAdminGenericProductSessionBean extends BasePizzaProductEnterpr
                }
 
                // Merge data
-               final Product managedProduct = this.mergeProductData(product);
+               final Product managedProduct = this.mergeGenericProductData(product);
 
                // Set updated instance
                managedProduct.setProductEntryUpdated(new Date());
index 172e500af0313d9318acf2b17af4287ff2084fff..f44dd4e5fe1072976223ac434a7353736a8169d3 100644 (file)
@@ -126,7 +126,7 @@ public abstract class BasePizzaProductEnterpriseBean extends BasePizzaEnterprise
         * <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
 
@@ -167,4 +167,52 @@ public abstract class BasePizzaProductEnterpriseBean extends BasePizzaEnterprise
                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;
+       }
+
 }