Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Determine 'create' or 'update' Entity(What is the best way to generically determine if an Entity is for 'create' or 'update'?)
Determine 'create' or 'update' Entity [message #1057811] Thu, 09 May 2013 01:05 Go to next message
Marvin Toll is currently offline Marvin TollFriend
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 #1057830 is a reply to message #1057811] Thu, 09 May 2013 07:05 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
On 2013-05-09 03:05, Marvin Toll wrote:
> 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?

I usually use a postload method to set a transient "loaded" boolean to true (default false). In that way you can determine if an entity was loaded from the datastore or created, which should be equivalent to update to insert.

Tom
Re: Determine 'create' or 'update' Entity [message #1057878 is a reply to message #1057830] Thu, 09 May 2013 13:57 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
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 #1062727 is a reply to message #1057878] Mon, 10 June 2013 14:28 Go to previous messageGo to next message
Marvin Toll is currently offline Marvin TollFriend
Messages: 34
Registered: July 2009
Member
Thank you @Chris... the Merge method is smarter than anticipated in determining if a merge is required.

However, when an attempt to (incorrectly) merge a duplicate key... an optimistic lock exception is thrown (instead of a duplicate key exception).

Any suggestions anyone?


Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com
Re: Determine 'create' or 'update' Entity [message #1062807 is a reply to message #1062727] Mon, 10 June 2013 19:45 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
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

Previous Topic:Validation exception on spurious insert during a query transaction
Next Topic:Null value if read the same @ManyToOne Entity
Goto Forum:
  


Current Time: Thu Apr 25 15:11:57 GMT 2024

Powered by FUDForum. Page generated in 0.04455 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top