From 309659f72e5bbda3df5214eab101a5f02dc679c9 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 --- .../BaseFinancialsProductEnterpriseBean.java | 51 ++++++++++++++++++- ...ncialsAdminProductCategorySessionBean.java | 13 ++--- ...ancialsAdminGenericProductSessionBean.java | 2 +- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/java/org/mxchange/jfinancials/enterprise/product/BaseFinancialsProductEnterpriseBean.java b/src/java/org/mxchange/jfinancials/enterprise/product/BaseFinancialsProductEnterpriseBean.java index 2dab641..afd47f5 100644 --- a/src/java/org/mxchange/jfinancials/enterprise/product/BaseFinancialsProductEnterpriseBean.java +++ b/src/java/org/mxchange/jfinancials/enterprise/product/BaseFinancialsProductEnterpriseBean.java @@ -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 *

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

+ * @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; + } + } diff --git a/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java b/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java index 0b0fe75..db6e507 100644 --- a/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java +++ b/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java @@ -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; } /** diff --git a/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java b/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java index 4cd0816..26aa418 100644 --- a/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java +++ b/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java @@ -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()); -- 2.39.5