From ab364e59de8b78bdba93a0e4d7945f1e7af20f37 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 25 Apr 2020 18:25:40 +0200 Subject: [PATCH] Product-only: - 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../PizzaAdminProductCategorySessionBean.java | 13 ++--- .../PizzaAdminGenericProductSessionBean.java | 2 +- .../BasePizzaProductEnterpriseBean.java | 50 ++++++++++++++++++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/java/org/mxchange/jproduct/model/category/PizzaAdminProductCategorySessionBean.java b/src/java/org/mxchange/jproduct/model/category/PizzaAdminProductCategorySessionBean.java index f85125b..b8a34a1 100644 --- a/src/java/org/mxchange/jproduct/model/category/PizzaAdminProductCategorySessionBean.java +++ b/src/java/org/mxchange/jproduct/model/category/PizzaAdminProductCategorySessionBean.java @@ -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; } /** diff --git a/src/java/org/mxchange/jproduct/model/product/PizzaAdminGenericProductSessionBean.java b/src/java/org/mxchange/jproduct/model/product/PizzaAdminGenericProductSessionBean.java index 3bc68ce..6e88679 100644 --- a/src/java/org/mxchange/jproduct/model/product/PizzaAdminGenericProductSessionBean.java +++ b/src/java/org/mxchange/jproduct/model/product/PizzaAdminGenericProductSessionBean.java @@ -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()); diff --git a/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java b/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java index 172e500..f44dd4e 100644 --- a/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java +++ b/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java @@ -126,7 +126,7 @@ public abstract class BasePizzaProductEnterpriseBean extends BasePizzaEnterprise *

* @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 + *

+ * @param detachedCategory Category instance to merge + *

+ * @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; + } + } -- 2.39.5