Skip to content

HHH-14413 fix issue that EntityUpdateAction increments version despite veto on update #3699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public void execute() throws HibernateException {
final SharedSessionContractImplementor session = getSession();
final Object instance = getInstance();

final boolean veto = preUpdate();
if ( preUpdate() ) {
return;
}

final SessionFactoryImplementor factory = session.getFactory();
Object previousVersion = this.previousVersion;
Expand All @@ -196,27 +198,24 @@ public void execute() throws HibernateException {
else {
ck = null;
}

if ( !veto ) {
persister.update(
id,
state,
dirtyFields,
hasDirtyCollection,
previousState,
previousVersion,
instance,
rowId,
session
);
}
persister.update(
id,
state,
dirtyFields,
hasDirtyCollection,
previousState,
previousVersion,
instance,
rowId,
session
);

final EntityEntry entry = session.getPersistenceContextInternal().getEntry( instance );
if ( entry == null ) {
throw new AssertionFailure( "possible nonthreadsafe access to session" );
throw new AssertionFailure( "possible non thread safe access to session" );
}

if ( entry.getStatus()==Status.MANAGED || persister.isVersionPropertyGenerated() ) {
if ( entry.getStatus() == Status.MANAGED || persister.isVersionPropertyGenerated() ) {
// get the updated snapshot of the entity state by cloning current state;
// it is safe to copy in place, since by this time no-one else (should have)
// has a reference to the array
Expand All @@ -242,12 +241,12 @@ public void execute() throws HibernateException {

final StatisticsImplementor statistics = factory.getStatistics();
if ( persister.canWriteToCache() ) {
if ( persister.isCacheInvalidationRequired() || entry.getStatus()!= Status.MANAGED ) {
persister.getCacheAccessStrategy().remove( session, ck);
if ( persister.isCacheInvalidationRequired() || entry.getStatus() != Status.MANAGED ) {
persister.getCacheAccessStrategy().remove( session, ck );
}
else if ( session.getCacheMode().isPutEnabled() ) {
//TODO: inefficient if that cache is just going to ignore the updated state!
final CacheEntry ce = persister.buildCacheEntry( instance,state, nextVersion, getSession() );
final CacheEntry ce = persister.buildCacheEntry( instance, state, nextVersion, getSession() );
cacheEntry = persister.getCacheEntryStructure().structure( ce );

final boolean put = cacheUpdate( persister, previousVersion, ck );
Expand All @@ -270,9 +269,10 @@ else if ( session.getCacheMode().isPutEnabled() ) {

postUpdate();

if ( statistics.isStatisticsEnabled() && !veto ) {
if ( statistics.isStatisticsEnabled() ) {
statistics.updateEntity( getPersister().getEntityName() );
}

}

protected boolean cacheUpdate(EntityPersister persister, Object previousVersion, Object ck) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.hibernate.event;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;

import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;

/**
* @author Nathan Xu
* @author Tassilo Karge
*/
@TestForIssue( jiraKey = "HHH-14413" )
public class PreUpdateEventListenerVetoTest extends BaseCoreFunctionalTestCase {

private static final Long EXAMPLE_ID_VALUE = 1L;

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { ExampleEntity.class };
}

@Override
protected void afterSessionFactoryBuilt() {
super.afterSessionFactoryBuilt();
EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService( EventListenerRegistry.class );
registry.appendListeners(
EventType.PRE_UPDATE,
event -> true
);
}

@Before
public void setUp() {
doInHibernate( this::sessionFactory, session -> {
ExampleEntity entity = new ExampleEntity();
entity.id = EXAMPLE_ID_VALUE;
entity.name = "old_name";
session.save( entity );
} );
}

@Test
public void testVersionNotChangedWhenPreUpdateEventVetoed() {

doInHibernate( this::sessionFactory, session -> {
ExampleEntity entity = session.byId( ExampleEntity.class ).load( EXAMPLE_ID_VALUE );

entity.name = "new_name";
session.update( entity );

final Long versionBeforeFlush = entity.version;

session.flush();

final Long versionAfterFlush = entity.version;

assertEquals( "The entity version must not change when update is vetoed", versionBeforeFlush, versionAfterFlush );

} );
}

@Entity(name = "ExampleEntity")
public static class ExampleEntity {

@Id
Long id;

String name;

@Version
Long version;

}
}