From 9686bd9011e30d6c3c4e48787beaf6d992eb04fd Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
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

---
 ...dressbookAdminContactPhoneSessionBean.java | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java b/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java
index fa71e34..1e11f92 100644
--- a/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java
+++ b/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java
@@ -97,8 +97,11 @@ public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookData
 		// 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 AddressbookAdminContactPhoneSessionBean extends BaseAddressbookData
 		// 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 AddressbookAdminContactPhoneSessionBean extends BaseAddressbookData
 		// 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