org.hibernate.action.internal.EntityVerifyVersionProcess Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.action.internal;
import org.hibernate.OptimisticLockException;
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
/**
* A BeforeTransactionCompletionProcess impl to verify an entity version as part of
* before-transaction-completion processing
*
* @author Scott Marlow
*/
public class EntityVerifyVersionProcess implements BeforeTransactionCompletionProcess {
private final Object object;
/**
* Constructs an EntityVerifyVersionProcess
*
* @param object The entity instance
*/
public EntityVerifyVersionProcess(Object object) {
this.object = object;
}
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
final EntityEntry entry = session.getPersistenceContext().getEntry( object );
// Don't check version for an entity that is not in the PersistenceContext;
if ( entry == null ) {
return;
}
final EntityPersister persister = entry.getPersister();
final Object latestVersion = persister.getCurrentVersion( entry.getId(), session );
if ( !entry.getVersion().equals( latestVersion ) ) {
throw new OptimisticLockException(
object,
"Newer version [" + latestVersion +
"] of entity [" + MessageHelper.infoString( entry.getEntityName(), entry.getId() ) +
"] found in database"
);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy