]> git.mxchange.org Git - addressbook-ejb.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Tue, 23 Aug 2016 15:50:42 +0000 (17:50 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 9 Jun 2017 20:09:22 +0000 (22:09 +0200)
- 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

src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java

index fa71e3403db6ec24439676f1a80c0e5e65df5c7c..1e11f923ce5a5db88a185fb2fc9f195a9a8da56f 100644 (file)
@@ -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