HHH-13256 - Fix the fieldsPreUpdateNeeded property index allocation in AbstractEntityPersister#update

This commit is contained in:
Vlad Mihalcea 2019-01-24 15:21:37 +02:00
parent 69af6caa95
commit 462e171ee3
5 changed files with 160 additions and 46 deletions

View File

@ -421,6 +421,12 @@ public final class ArrayHelper {
return destination; return destination;
} }
public static int[] trim(int[] from, int length) {
int[] trimmed = new int[length];
System.arraycopy( from, 0, trimmed, 0, length );
return trimmed;
}
@AllowSysOut @AllowSysOut
public static void main(String... args) { public static void main(String... args) {
int[] batchSizes = ArrayHelper.getBatchSizes( 32 ); int[] batchSizes = ArrayHelper.getBatchSizes( 32 );

View File

@ -3546,6 +3546,7 @@ public abstract class AbstractEntityPersister
int valueGenerationStrategiesSize = valueGenerationStrategies.length; int valueGenerationStrategiesSize = valueGenerationStrategies.length;
if ( valueGenerationStrategiesSize != 0 ) { if ( valueGenerationStrategiesSize != 0 ) {
int[] fieldsPreUpdateNeeded = new int[valueGenerationStrategiesSize]; int[] fieldsPreUpdateNeeded = new int[valueGenerationStrategiesSize];
int count = 0;
for ( int i = 0; i < valueGenerationStrategiesSize; i++ ) { for ( int i = 0; i < valueGenerationStrategiesSize; i++ ) {
if ( valueGenerationStrategies[i] != null && valueGenerationStrategies[i].getGenerationTiming() if ( valueGenerationStrategies[i] != null && valueGenerationStrategies[i].getGenerationTiming()
.includesUpdate() ) { .includesUpdate() ) {
@ -3554,7 +3555,7 @@ public abstract class AbstractEntityPersister
object object
); );
setPropertyValue( object, i, fields[i] ); setPropertyValue( object, i, fields[i] );
fieldsPreUpdateNeeded[i] = i; fieldsPreUpdateNeeded[count++] = i;
} }
} }
// if ( fieldsPreUpdateNeeded.length != 0 ) { // if ( fieldsPreUpdateNeeded.length != 0 ) {
@ -3567,7 +3568,7 @@ public abstract class AbstractEntityPersister
// // no dirty fields and no dirty collections so no update needed ??? // // no dirty fields and no dirty collections so no update needed ???
// } // }
if ( fieldsPreUpdateNeeded.length != 0 && dirtyFields != null ) { if ( fieldsPreUpdateNeeded.length != 0 && dirtyFields != null ) {
dirtyFields = ArrayHelper.join( fieldsPreUpdateNeeded, dirtyFields ); dirtyFields = ArrayHelper.join( dirtyFields, ArrayHelper.trim( fieldsPreUpdateNeeded, count ) );
} }
} }
} }

View File

@ -8,6 +8,7 @@ package org.hibernate.type;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer; import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl; import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
import org.hibernate.tuple.NonIdentifierAttribute; import org.hibernate.tuple.NonIdentifierAttribute;
@ -325,9 +326,7 @@ public class TypeHelper {
return null; return null;
} }
else { else {
int[] trimmed = new int[count]; return ArrayHelper.trim(results, count);
System.arraycopy( results, 0, trimmed, 0, count );
return trimmed;
} }
} }

View File

@ -25,18 +25,18 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@TestForIssue( jiraKey = "HHH-11096" ) @TestForIssue(jiraKey = "HHH-11096")
public class InMemoryCreationTimestampNullableColumnTest extends BaseEntityManagerFunctionalTestCase { public class InMemoryCreationTimestampNullableColumnTest extends BaseEntityManagerFunctionalTestCase {
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{ return new Class<?>[] {
Person.class Person.class
}; };
} }
@Entity(name = "Person") @Entity(name = "Person")
public class Person { public static class Person {
@Id @Id
@GeneratedValue @GeneratedValue
@ -64,18 +64,17 @@ public class InMemoryCreationTimestampNullableColumnTest extends BaseEntityManag
public void setCreationDate(Date creationDate) { public void setCreationDate(Date creationDate) {
this.creationDate = creationDate; this.creationDate = creationDate;
} }
} }
@Test @Test
public void generatesCurrentTimestamp() { public void generatesCurrentTimestamp() {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
Person person = new Person(); Person person = new Person();
person.setName("John Doe"); person.setName( "John Doe" );
entityManager.persist(person); entityManager.persist( person );
entityManager.flush(); entityManager.flush();
Assert.assertNotNull(person.getCreationDate()); Assert.assertNotNull( person.getCreationDate() );
}); } );
} }
} }

View File

@ -0,0 +1,109 @@
/*
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.annotations;
import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertTrue;
/**
* @author Vlad Mihalcea
*/
@TestForIssue(jiraKey = "HHH-13256")
public class InMemoryUpdateTimestampTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class
};
}
@Test
public void test() {
doInJPA( this::entityManagerFactory, entityManager -> {
Person person = new Person();
person.setId( 1L );
person.setFirstName( "Jon" );
person.setLastName( "Doe" );
entityManager.persist( person );
entityManager.flush();
Assert.assertNotNull( person.getUpdatedOn() );
} );
AtomicReference<Date> beforeTimestamp = new AtomicReference<>();
sleep( 1 );
Person _person = doInJPA( this::entityManagerFactory, entityManager -> {
Person person = entityManager.find( Person.class, 1L );
beforeTimestamp.set( person.getUpdatedOn() );
person.setLastName( "Doe Jr." );
return person;
} );
assertTrue( _person.getUpdatedOn().after( beforeTimestamp.get() ) );
}
@Entity(name = "Person")
public static class Person {
@Id
private Long id;
private String firstName;
private String lastName;
@Column(nullable = false)
@UpdateTimestamp
private Date updatedOn;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getUpdatedOn() {
return updatedOn;
}
}
}