From f6f25934b2950fee04d7c728ebb9ed7b255837d6 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Wed, 24 Aug 2016 13:09:01 +0200
Subject: [PATCH] Please cherry-pick: - sadly all getReference() + merge() was
 based on wrong assumption, now find() returns a managed entity where all
 setter calls leads to an update - used getManaged() of repeating code (DRY)

---
 .../user/AddressbookUserSessionBean.java      | 120 +++++-------------
 1 file changed, 30 insertions(+), 90 deletions(-)

diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
index 6221f37..c360927 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
@@ -573,28 +573,22 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		user.setUserContact(null);
 
 		// Find the instance
-		User foundUser = this.getEntityManager().getReference(user.getClass(), user.getUserId());
+		User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
 
 		// Should be found!
-		assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
-
-		// Merge user
-		User detachedUser = this.getEntityManager().merge(foundUser);
-
-		// Should be found!
-		assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
+		assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
 
 		// Copy all data
-		detachedUser.copyAll(user);
+		managedUser.copyAll(user);
 
 		// Set as updated
-		detachedUser.setUserUpdated(new GregorianCalendar());
+		managedUser.setUserUpdated(new GregorianCalendar());
 
 		// Trace message
-		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - CALLED!", this.getClass().getSimpleName(), managedUser)); //NOI18N
 
 		// Return updated instance
-		return detachedUser;
+		return managedUser;
 	}
 
 	@Override
@@ -672,131 +666,77 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		}
 
 		// Find the instance
-		User foundUser = this.getEntityManager().getReference(user.getClass(), user.getUserId());
-
-		// Should be found!
-		assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
-
-		// Merge user
-		User detachedUser = this.getEntityManager().merge(foundUser);
+		User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
 
 		// Should be found!
-		assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
+		assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
 
 		// Copy all data
-		detachedUser.copyAll(user);
+		managedUser.copyAll(user);
 
 		// Set as updated
-		detachedUser.setUserUpdated(new GregorianCalendar());
-		detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
+		managedUser.setUserUpdated(new GregorianCalendar());
+		managedUser.getUserContact().setContactUpdated(new GregorianCalendar());
 
 		// Get contact from it and find it
-		Contact foundContact = this.getEntityManager().getReference(user.getUserContact().getClass(), user.getUserContact().getContactId());
+		Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
 
 		// Should be found
-		assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
+		assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
 
 		// Debug message
-		this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
-
-		// Merge contact instance
-		Contact detachedContact = this.getEntityManager().merge(foundContact);
+		this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: managedContact.contactId={0}", managedContact.getContactId())); //NOI18N
 
 		// Copy all
-		detachedContact.copyAll(user.getUserContact());
+		managedContact.copyAll(user.getUserContact());
 
 		// Set it back in user
-		user.setUserContact(detachedContact);
+		user.setUserContact(managedContact);
 
 		// Should be found!
-		assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
+		assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
 
 		// Get mobile instance
-		DialableMobileNumber mobileNumber = detachedContact.getContactMobileNumber();
+		DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
 
 		// Is there a  mobile instance set?
 		if (mobileNumber instanceof DialableMobileNumber) {
 			// Debug message
-			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
-
-			// Then find it, too
-			DialableMobileNumber foundMobile = this.getEntityManager().getReference(mobileNumber.getClass(), mobileNumber.getPhoneId());
-
-			// Should be there
-			assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
-
-			// Then merge it, too
-			DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile);
-
-			// Should be there
-			assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N
-
-			// Copy all
-			detachedMobile.copyAll(user.getUserContact().getContactMobileNumber());
+			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobileNumber.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
 
 			// Set it back
-			detachedContact.setContactMobileNumber(detachedMobile);
+			managedContact.setContactMobileNumber(this.getManaged(mobileNumber, mobileNumber));
 		}
 
 		// Get mobile instance
-		DialableFaxNumber fax = detachedContact.getContactFaxNumber();
+		DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
 
 		// Is there a  fax instance set?
-		if (fax instanceof DialableFaxNumber) {
+		if (faxNumber instanceof DialableFaxNumber) {
 			// Debug message
-			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
-
-			// Then find it, too
-			DialableFaxNumber foundFax = this.getEntityManager().getReference(fax.getClass(), fax.getPhoneId());
-
-			// Should be there
-			assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
-
-			// Then merge it, too
-			DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
-
-			// Should be there
-			assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
-
-			// Copy all
-			detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
+			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
 
 			// Set it back
-			detachedContact.setContactFaxNumber(detachedFax);
+			managedContact.setContactFaxNumber(this.getManaged(faxNumber, faxNumber));
 		}
 
 		// Get mobile instance
-		DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
+		DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
 
 		// Is there a  fax instance set?
-		if (landLine instanceof DialableLandLineNumber) {
+		if (landLineNumber instanceof DialableLandLineNumber) {
 			// Debug message
-			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
-
-			// Then find it, too
-			DialableLandLineNumber foundLandLine = this.getEntityManager().getReference(landLine.getClass(), landLine.getPhoneId());
-
-			// Should be there
-			assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
-
-			// Then merge it, too
-			DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
-
-			// Should be there
-			assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
-
-			// Copy all
-			detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
+			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
 
 			// Set it back
-			detachedContact.setContactLandLineNumber(detachedLandLine);
+			managedContact.setContactLandLineNumber(this.getManaged(landLineNumber, landLineNumber));
 		}
 
 		// Trace message
-		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
 
 		// Return updated user instance
-		return detachedUser;
+		return managedUser;
 	}
 
 }
-- 
2.39.5