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;
}
public static int[] trim(int[] from, int length) {
int[] trimmed = new int[length];
System.arraycopy( from, 0, trimmed, 0, length );
return trimmed;
}
@AllowSysOut
public static void main(String... args) {
int[] batchSizes = ArrayHelper.getBatchSizes( 32 );

View File

@ -3546,6 +3546,7 @@ public abstract class AbstractEntityPersister
int valueGenerationStrategiesSize = valueGenerationStrategies.length;
if ( valueGenerationStrategiesSize != 0 ) {
int[] fieldsPreUpdateNeeded = new int[valueGenerationStrategiesSize];
int count = 0;
for ( int i = 0; i < valueGenerationStrategiesSize; i++ ) {
if ( valueGenerationStrategies[i] != null && valueGenerationStrategies[i].getGenerationTiming()
.includesUpdate() ) {
@ -3554,7 +3555,7 @@ public abstract class AbstractEntityPersister
object
);
setPropertyValue( object, i, fields[i] );
fieldsPreUpdateNeeded[i] = i;
fieldsPreUpdateNeeded[count++] = i;
}
}
// if ( fieldsPreUpdateNeeded.length != 0 ) {
@ -3567,7 +3568,7 @@ public abstract class AbstractEntityPersister
// // no dirty fields and no dirty collections so no update needed ???
// }
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.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
import org.hibernate.tuple.NonIdentifierAttribute;
@ -325,9 +326,7 @@ public class TypeHelper {
return null;
}
else {
int[] trimmed = new int[count];
System.arraycopy( results, 0, trimmed, 0, count );
return trimmed;
return ArrayHelper.trim(results, count);
}
}

View File

@ -25,57 +25,56 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
/**
* @author Vlad Mihalcea
*/
@TestForIssue( jiraKey = "HHH-11096" )
@TestForIssue(jiraKey = "HHH-11096")
public class InMemoryCreationTimestampNullableColumnTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{
Person.class
};
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class
};
}
@Entity(name = "Person")
public class Person {
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@Id
@GeneratedValue
private Long id;
@NaturalId
private String name;
@NaturalId
private String name;
@Column(nullable = false)
@CreationTimestamp
private Date creationDate;
@Column(nullable = false)
@CreationTimestamp
private Date creationDate;
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreationDate() {
return creationDate;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
}
}
@Test
public void generatesCurrentTimestamp() {
doInJPA( this::entityManagerFactory, entityManager -> {
Person person = new Person();
person.setName( "John Doe" );
entityManager.persist( person );
@Test
public void generatesCurrentTimestamp() {
doInJPA(this::entityManagerFactory, entityManager -> {
Person person = new Person();
person.setName("John Doe");
entityManager.persist(person);
entityManager.flush();
Assert.assertNotNull(person.getCreationDate());
});
}
entityManager.flush();
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;
}
}
}