HHH-11585 - Batch ordering fails for bidirectional one-to-one associations

This commit is contained in:
Vlad Mihalcea 2017-03-22 10:43:22 +02:00
parent c8cbb8f0c6
commit f90845c30c
4 changed files with 262 additions and 21 deletions

View File

@ -44,6 +44,7 @@ import org.hibernate.cache.CacheException;
import org.hibernate.engine.internal.NonNullableTransientDependencies;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.type.CollectionType;
@ -1134,23 +1135,32 @@ public class ActionQueue {
*/
private void addParentChildEntityNames(AbstractEntityInsertAction action, BatchIdentifier batchIdentifier) {
Object[] propertyValues = action.getState();
Type[] propertyTypes = action.getPersister().getClassMetadata().getPropertyTypes();
ClassMetadata classMetadata = action.getPersister().getClassMetadata();
if ( classMetadata != null ) {
Type[] propertyTypes = classMetadata.getPropertyTypes();
for ( int i = 0; i < propertyValues.length; i++ ) {
Object value = propertyValues[i];
Type type = propertyTypes[i];
if ( type.isEntityType() && value != null ) {
EntityType entityType = (EntityType) type;
String entityName = entityType.getName();
batchIdentifier.getParentEntityNames().add( entityName );
}
else if ( type.isCollectionType() && value != null ) {
CollectionType collectionType = (CollectionType) type;
final SessionFactoryImplementor sessionFactory = ( (SessionImplementor) action.getSession() )
.getSessionFactory();
if ( collectionType.getElementType( sessionFactory ).isEntityType() ) {
String entityName = collectionType.getAssociatedEntityName( sessionFactory );
batchIdentifier.getChildEntityNames().add( entityName );
for ( int i = 0; i < propertyValues.length; i++ ) {
Object value = propertyValues[i];
Type type = propertyTypes[i];
if ( type.isEntityType() && value != null ) {
EntityType entityType = (EntityType) type;
String entityName = entityType.getName();
if ( entityType.isOneToOne() ) {
batchIdentifier.getChildEntityNames().add( entityName );
}
else {
batchIdentifier.getParentEntityNames().add( entityName );
}
}
else if ( type.isCollectionType() && value != null ) {
CollectionType collectionType = (CollectionType) type;
final SessionFactoryImplementor sessionFactory = ( (SessionImplementor) action.getSession() )
.getSessionFactory();
if ( collectionType.getElementType( sessionFactory ).isEntityType() ) {
String entityName = collectionType.getAssociatedEntityName( sessionFactory );
batchIdentifier.getChildEntityNames().add( entityName );
}
}
}
}

View File

@ -0,0 +1,111 @@
/*
* 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.insertordering;
import org.hibernate.cfg.Environment;
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import javax.persistence.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Vlad Mihalcea
*/
@TestForIssue(jiraKey = "HHH-9864")
public class InsertOrderingWithBidirectionalOneToOne
extends BaseNonConfigCoreFunctionalTestCase {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Address.class, Person.class };
}
@Override
protected void addSettings(Map settings) {
settings.put( Environment.ORDER_INSERTS, "true" );
settings.put( Environment.STATEMENT_BATCH_SIZE, "10" );
settings.put(
org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER,
connectionProvider
);
}
@Override
public void releaseResources() {
super.releaseResources();
connectionProvider.stop();
}
@Test
public void testBatching() throws SQLException {
doInHibernate( this::sessionFactory, session -> {
Person worker = new Person();
Person homestay = new Person();
Address home = new Address();
Address office = new Address();
home.addPerson( homestay );
office.addPerson( worker );
session.persist( home );
session.persist( office );
connectionProvider.clear();
} );
PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement(
"insert into Address (ID) values (?)" );
verify( addressPreparedStatement, times( 2 ) ).addBatch();
PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement(
"insert into Person (address_ID, ID) values (?, ?)" );
verify( personPreparedStatement, times( 2 ) ).addBatch();
}
@Entity(name = "Address")
public static class Address {
@Id
@Column(name = "ID", nullable = false)
@SequenceGenerator(name = "ID", sequenceName = "ADDRESS_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID")
private Long id;
@OneToOne(mappedBy = "address", cascade = CascadeType.PERSIST)
private Person person;
public void addPerson(Person person) {
this.person = person;
person.address = this;
}
}
@Entity(name = "Person")
public static class Person {
@Id
@Column(name = "ID", nullable = false)
@SequenceGenerator(name = "ID", sequenceName = "ADDRESS_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID")
private Long id;
@OneToOne
private Address address;
}
}

View File

@ -0,0 +1,113 @@
/*
* 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.insertordering;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Vlad Mihalcea
*/
@TestForIssue(jiraKey = "HHH-9864")
public class InsertOrderingWithUnidirectionalOneToOne
extends BaseNonConfigCoreFunctionalTestCase {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Address.class, Person.class };
}
@Override
protected void addSettings(Map settings) {
settings.put( Environment.ORDER_INSERTS, "true" );
settings.put( Environment.STATEMENT_BATCH_SIZE, "10" );
settings.put(
org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER,
connectionProvider
);
}
@Override
public void releaseResources() {
super.releaseResources();
connectionProvider.stop();
}
@Test
public void testBatching() throws SQLException {
doInHibernate( this::sessionFactory, session -> {
Person worker = new Person();
Person homestay = new Person();
Address home = new Address();
Address office = new Address();
home.addPerson( homestay );
office.addPerson( worker );
session.persist( home );
session.persist( office );
connectionProvider.clear();
} );
PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement(
"insert into Address (person_ID, ID) values (?, ?)" );
verify( addressPreparedStatement, times( 2 ) ).addBatch();
PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement(
"insert into Person (ID) values (?)" );
verify( personPreparedStatement, times( 2 ) ).addBatch();
}
@Entity(name = "Address")
public static class Address {
@Id
@Column(name = "ID", nullable = false)
@SequenceGenerator(name = "ID", sequenceName = "ADDRESS_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID")
private Long id;
@OneToOne( cascade = CascadeType.PERSIST )
private Person person;
public void addPerson(Person person) {
this.person = person;
}
}
@Entity(name = "Person")
public static class Person {
@Id
@Column(name = "ID", nullable = false)
@SequenceGenerator(name = "ID", sequenceName = "ADDRESS_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID")
private Long id;
}
}

View File

@ -9,11 +9,7 @@ package org.hibernate.test.legacy;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
@ -21,6 +17,8 @@ import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.hibernate.dialect.HSQLDialect;
@ -54,6 +52,15 @@ public class MasterDetailTest extends LegacyTestCase {
};
}
@Override
public void configure(Configuration cfg) {
super.configure(cfg);
Properties props = new Properties();
props.put( Environment.ORDER_INSERTS, "true" );
props.put( Environment.STATEMENT_BATCH_SIZE, "10" );
cfg.addProperties( props );
}
@Test
public void testOuterJoin() throws Exception {
Session s = openSession();