Use JTD to compare row results in ListResultConsumer

This commit is contained in:
Andrea Boriero 2021-06-03 14:31:09 +02:00
parent a8cf8165ae
commit d1bc4e6a33
3 changed files with 35 additions and 10 deletions

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.sql.results.internal;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -22,6 +23,7 @@ import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState;
import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
import org.hibernate.sql.results.spi.RowReader;
import org.hibernate.sql.results.spi.RowTransformer;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* @author Steve Ebersole
@ -71,6 +73,15 @@ public class StandardRowReader<T> implements RowReader<T> {
return (Class<T>) Object[].class;
}
@Override
public List<JavaTypeDescriptor> getResultJavaTypeDescriptors() {
List<JavaTypeDescriptor> javaTypeDescriptors = new ArrayList<>( resultAssemblers.size() );
for ( DomainResultAssembler resultAssembler : resultAssemblers ) {
javaTypeDescriptors.add( resultAssembler.getAssembledJavaTypeDescriptor() );
}
return javaTypeDescriptors;
}
@Override
public List<Initializer> getInitializers() {
return initializers;

View File

@ -80,21 +80,29 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
uniqueRows = true;
}
}
final List<JavaTypeDescriptor> resultJavaTypeDescriptors = rowReader.getResultJavaTypeDescriptors();
final JavaTypeDescriptor<R> resultJavaTypeDescriptor = resultJavaTypeDescriptors.get( 0 );
while ( rowProcessingState.next() ) {
final R row = rowReader.readRow( rowProcessingState, processingOptions );
boolean add = true;
if ( uniqueRows ) {
if ( results.contains( row ) ) {
if ( uniqueSemantic == UniqueSemantic.ASSERT && !rowProcessingState.hasCollectionInitializers() ) {
throw new HibernateException(
"More than one row with the given identifier was found: " +
jdbcValuesSourceProcessingState.getExecutionContext()
.getEntityId() +
", for class: " +
resultJavaType.getName()
);
assert resultJavaTypeDescriptors.size() == 1;
for ( R existingRow : results ) {
if ( resultJavaTypeDescriptor.areEqual( existingRow, row ) ) {
if ( uniqueSemantic == UniqueSemantic.ASSERT && !rowProcessingState.hasCollectionInitializers() ) {
throw new HibernateException(
"More than one row with the given identifier was found: " +
jdbcValuesSourceProcessingState.getExecutionContext()
.getEntityId() +
", for class: " +
resultJavaType.getName()
);
}
add = false;
break;
}
add = false;
}
}
if ( add ) {

View File

@ -15,6 +15,7 @@ import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState;
import org.hibernate.query.named.RowReaderMemento;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* Coordinates the process of reading a single result values row
@ -29,6 +30,11 @@ public interface RowReader<R> {
*/
Class<R> getResultJavaType();
/**
* The JavaTypeDescriptors of the result
*/
List<JavaTypeDescriptor> getResultJavaTypeDescriptors();
/**
* The initializers associated with this reader
*/