- 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
// Find contact
Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
// 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
// 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
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
// Find contact
Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
// 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
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
// Find contact
Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
// 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
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N