From c4c79669a44e65d8f08f9a7d4f34167274121169 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 23 Aug 2016 17:50:42 +0200 Subject: [PATCH] Please cherry-pick: - if you don't want to have redundant entries (=objects aka. entities) in database of referenced entities, first you need to merge it as it is detached - then you have a managed instance which you can set in the referencing entity back - if you don't do this, the entity manager will "think" it is a new instance and persists + relinks it - maybe some implementations don't do this, but the JPA standard (generic) seems to want to have this way - do never implement for only one JPA implementation! Do always follow the generic JPA way --- .../JobsAdminContactPhoneSessionBean.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/java/org/mxchange/jcontacts/phone/JobsAdminContactPhoneSessionBean.java b/src/java/org/mxchange/jcontacts/phone/JobsAdminContactPhoneSessionBean.java index 31f565b..d8d4beb 100644 --- a/src/java/org/mxchange/jcontacts/phone/JobsAdminContactPhoneSessionBean.java +++ b/src/java/org/mxchange/jcontacts/phone/JobsAdminContactPhoneSessionBean.java @@ -97,8 +97,11 @@ public class JobsAdminContactPhoneSessionBean extends BaseJobsDatabaseBean imple // Find contact Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId()); + // Merge phone number + DialableFaxNumber managedNumber = this.getEntityManager().merge(faxNumber); + // Set fax number in contact - managedContact.setContactFaxNumber(faxNumber); + managedContact.setContactFaxNumber(managedNumber); // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N @@ -154,8 +157,11 @@ public class JobsAdminContactPhoneSessionBean extends BaseJobsDatabaseBean imple // Find contact Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId()); - // Set landline number in contact - managedContact.setContactLandLineNumber(landLineNumber); + // Merge phone number + DialableLandLineNumber managedNumber = this.getEntityManager().merge(landLineNumber); + + // Set fax number in contact + managedContact.setContactLandLineNumber(managedNumber); // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N @@ -205,8 +211,11 @@ public class JobsAdminContactPhoneSessionBean extends BaseJobsDatabaseBean imple // Find contact Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId()); - // Set landline number in contact - managedContact.setContactMobileNumber(mobileNumber); + // Merge phone number + DialableMobileNumber managedNumber = this.getEntityManager().merge(mobileNumber); + + // Set fax number in contact + managedContact.setContactMobileNumber(managedNumber); // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N -- 2.39.5