Determine 'create' or 'update' Entity [message #1057811] |
Thu, 09 May 2013 01:05 |
Marvin Toll Messages: 34 Registered: July 2009 |
Member |
|
|
The following generic method appears to warrant determination if a 'create' or 'update' is being persisted. Is there a way to make that determination? Or, is there fundamentally a better approach?
/**
* This method executes the persist invocation. A merge is included if the
* entity is an update.
*
* @param entity
* @param entityManager
*/
protected <ENTITY extends SjcBaseDE> void persistEntity(ENTITY entity,
final EntityManager entityManager) {
final String Method_Name = "persistEntity";
this.logger.entering(this.Class_Name, Method_Name);
final boolean isUpdate = false;
// How does one determine if this is a create or update?
// Is there anything in session to help with this?
final Session session = ((EntityManagerImpl) entityManager)
.getActiveSession();
try {
if (isUpdate) {
entity = mergeIfRequired(entityManager, entity);
}
} catch (final Exception e) {
throw new SjpPersistenceEPC(new SjcExceptionPathPO(this.Class_Name,
Method_Name), "The entity ["
+ entity.getClass().getSimpleName()
+ "] is not a valid persistence audit entity.");
}
try {
entityManager.persist(entity);
logPersist(Method_Name, entity, isUpdate);
} catch (final Exception e) {
throw new SjpPersistenceEPC(new SjcExceptionPathPO(this.Class_Name,
Method_Name), "Unable to persist ["
+ entity.getClass().getSimpleName() + "] instance.", );
}
this.logger.exiting(this.Class_Name, Method_Name);
return;
}
Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com
|
|
|
|
Re: Determine 'create' or 'update' Entity [message #1057878 is a reply to message #1057830] |
Thu, 09 May 2013 13:57 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
If you don't know, why bother checking and just call merge which will do the check for you. Merge will find the entity if it exists and merge into it, otherwise it will end up inserting the entity. Persist can be more efficient if you know the entity is new because it doesn't need the check. The other difference for new entities is that Persist makes the passed in instance managed, while merge will return a managed instance which will be a different instance than what is passed in. This might be important if you want to get the generated pk value afterward.
If you must be able to tell, merge needs to bring in the entity anyway, so you can do the same by calling em.find. If it doesn't have a pk, it can't exist, so you should be able to assume it is new. If it has a version number and a pk, you might not want to bother with calling find, since it was likely read in from the EM and it isn't needed.
[Updated on: Thu, 09 May 2013 13:57] Report message to a moderator
|
|
|
|
Re: Determine 'create' or 'update' Entity [message #1062807 is a reply to message #1062727] |
Mon, 10 June 2013 19:45 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
EclipseLink is required to throw the optimistic lock exception since it finds an existing entity with that ID with a later version number/field. Only your app knows that it intended to insert the object rather than merge the changes into the existing object. I'd recommend catching the exception and checking if the version field is null on the passed in object, and throw your own exception if it is. If you are unsure of the object that might be causing it (ie any object in the tree), you might try getting the underlying EclipseLink OptimisticLockException, pull the entity from it to check if it was intended to be new (using the version attribute if need be) and throw the application required exception. You can get the object from the org.eclipse.persistence.exceptions.OptimisticLockException using getObject().
Best Regards,
Chris
[Updated on: Mon, 10 June 2013 19:45] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03902 seconds