Re-enabled additional test and fix issue with native queries not flushing session

This commit is contained in:
Andrea Boriero 2020-11-25 17:05:01 +01:00
parent e3947b3b1c
commit 1a9bd7d4b3
11 changed files with 107 additions and 11 deletions

View File

@ -37,7 +37,8 @@ public class NativeQueryInterpreterStandardImpl implements NativeQueryInterprete
queryDefinition.getAffectedTableNames(),
queryDefinition.getQueryParameterList(),
queryDefinition.getJdbcValuesMappingProducer(),
queryDefinition.getRowTransformer()
queryDefinition.getRowTransformer(),
sessionFactory
);
}
}

View File

@ -46,7 +46,8 @@ public interface NativeQueryInterpreter extends Service {
queryDefinition.getAffectedTableNames(),
queryDefinition.getQueryParameterList(),
queryDefinition.getJdbcValuesMappingProducer(),
queryDefinition.getRowTransformer()
queryDefinition.getRowTransformer(),
sessionFactory
);
}

View File

@ -788,6 +788,9 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
.getNativeQueryMemento( queryName );
if ( namedNativeDescriptor != null ) {
if( resultType == null){
resultType = (Class<T>) namedNativeDescriptor.getResultMappingClass();
}
NativeQueryImplementor query = namedNativeDescriptor.toQuery( this, resultType );
query.setComment( "dynamic native SQL query" );
applyQuerySettingsAndHints( query );

View File

@ -8,12 +8,15 @@ package org.hibernate.metamodel.mapping.internal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping;
import org.hibernate.metamodel.mapping.AssociationKey;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.ColumnConsumer;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.metamodel.mapping.EntityMappingType;
@ -49,7 +52,7 @@ import org.hibernate.type.spi.TypeConfiguration;
*/
public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, ModelPart {
private final EmbeddableValuedModelPart mappingType;
private final AbstractCompositeIdentifierMapping mappingType;
private final String keyColumnContainingTable;
private final List<String> keyColumnExpressions;
private final String targetColumnContainingTable;
@ -170,7 +173,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
final TableReference tableReference = tableGroup.resolveTableReference( keyColumnContainingTable );
final String identificationVariable = tableReference.getIdentificationVariable();
int size = keyColumnExpressions.size();
List<SqlSelection> sqlSelections = new ArrayList<>(size);
List<SqlSelection> sqlSelections = new ArrayList<>( size );
for ( int i = 0; i < size; i++ ) {
final String columnExpression = keyColumnExpressions.get( i );
final JdbcMapping jdbcMapping = jdbcMappings.get( i );
@ -395,7 +398,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
final TableReference tableReference = tableGroup.resolveTableReference( keyColumnContainingTable );
final String identificationVariable = tableReference.getIdentificationVariable();
int size = keyColumnExpressions.size();
List<SqlSelection> sqlSelections = new ArrayList<>(size);
List<SqlSelection> sqlSelections = new ArrayList<>( size );
for ( int i = 0; i < size; i++ ) {
final String columnExpression = keyColumnExpressions.get( i );
final JdbcMapping jdbcMapping = jdbcMappings.get( i );
@ -443,4 +446,18 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
Consumer<JdbcMapping> action, Clause clause, TypeConfiguration typeConfiguration) {
mappingType.visitJdbcTypes( action, clause, typeConfiguration );
}
@Override
public void visitDisassembledJdbcValues(
Object value,
Clause clause,
JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) {
mappingType.visitDisassembledJdbcValues( value, clause, valuesConsumer, session );
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
return mappingType.disassemble( value, session );
}
}

View File

@ -7,16 +7,19 @@
package org.hibernate.metamodel.mapping.internal;
import java.util.Collection;
import java.util.Iterator;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.SingularAttributeMapping;
import org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.sql.ast.Clause;
/**
* Support for {@link javax.persistence.EmbeddedId}
@ -108,4 +111,28 @@ public class EmbeddedIdentifierMappingImpl extends AbstractCompositeIdentifierMa
return name;
}
@Override
public void visitDisassembledJdbcValues(
Object value,
Clause clause,
JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) {
getEmbeddableTypeDescriptor().visitDisassembledJdbcValues( value, clause, valuesConsumer, session );
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
final Collection<SingularAttributeMapping> attributeMappings = getAttributes();
Object[] result = new Object[attributeMappings.size()];
int i = 0;
final Iterator<SingularAttributeMapping> iterator = attributeMappings.iterator();
while ( iterator.hasNext() ) {
AttributeMapping mapping = iterator.next();
Object o = mapping.getPropertyAccess().getGetter().get( value );
result[i] = mapping.disassemble( o, session );
i++;
}
return result;
}
}

View File

@ -6,11 +6,14 @@
*/
package org.hibernate.query.results;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@ -18,7 +21,10 @@ import org.hibernate.Incubating;
import org.hibernate.Internal;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.named.NamedResultSetMappingMemento;
import org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy;
import org.hibernate.sql.ast.spi.SqlSelection;
@ -101,6 +107,19 @@ public class ResultSetMappingImpl implements ResultSetMapping {
}
}
@Override
public void addAffectedTableNames(Set<String> affectedTableNames, SessionFactoryImplementor sessionFactory) {
if ( StringHelper.isEmpty( mappingIdentifier ) ) {
return;
}
EntityPersister entityDescriptor = sessionFactory.getMetamodel().findEntityDescriptor( mappingIdentifier );
if ( entityDescriptor == null ) {
return;
}
Collections.addAll( affectedTableNames, (String[]) entityDescriptor.getQuerySpaces());
}
@Override
public JdbcValuesMapping resolve(
JdbcValuesMetadata jdbcResultsMetadata,

View File

@ -208,7 +208,10 @@ public class NativeQueryImpl<R>
SharedSessionContractImplementor session) {
this(
memento,
() -> new ResultSetMappingImpl( resultJavaType.getName() ),
() -> {
final String mappingIdentifier = resultJavaType != null ? resultJavaType.getName() : null;
return new ResultSetMappingImpl( mappingIdentifier );
},
(resultSetMapping, querySpaceConsumer, context) -> {
if ( resultJavaType != null ) {

View File

@ -8,10 +8,12 @@ package org.hibernate.query.sql.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.ScrollMode;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.model.domain.AllowableParameterType;
@ -50,14 +52,21 @@ public class NativeSelectQueryPlanImpl<R> implements NativeSelectQueryPlan<R> {
Set<String> affectedTableNames,
List<QueryParameterImplementor<?>> parameterList,
JdbcValuesMappingProducer resultSetMapping,
RowTransformer<R> rowTransformer) {
RowTransformer<R> rowTransformer,
SessionFactoryImplementor sessionFactory) {
this.sql = sql;
this.affectedTableNames = affectedTableNames;
this.parameterList = parameterList;
this.resultSetMapping = resultSetMapping;
this.rowTransformer = rowTransformer != null
? rowTransformer
: RowTransformerPassThruImpl.instance();
if ( affectedTableNames == null ) {
affectedTableNames = new HashSet<>();
}
if ( resultSetMapping != null ) {
resultSetMapping.addAffectedTableNames( affectedTableNames, sessionFactory );
}
this.affectedTableNames = affectedTableNames;
}
@Override
@ -97,6 +106,8 @@ public class NativeSelectQueryPlanImpl<R> implements NativeSelectQueryPlan<R> {
);
}
executionContext.getSession().autoFlushIfRequired( affectedTableNames );
final JdbcSelect jdbcSelect = new JdbcSelect(
sql,
jdbcParameterBinders,

View File

@ -7,6 +7,7 @@
package org.hibernate.sql.results.jdbc.internal;
import java.util.List;
import java.util.Set;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.sql.ast.spi.SqlSelection;
@ -32,6 +33,11 @@ public class JdbcValuesMappingProducerStandard implements JdbcValuesMappingProdu
resolvedMapping = new StandardJdbcValuesMapping( sqlSelections, domainResults );
}
// @Override
// public void addAffectedTableNames(Set<String> affectedTableNames, SessionFactoryImplementor sessionFactory) {
//
// }
@Override
public JdbcValuesMapping resolve(
JdbcValuesMetadata jdbcResultsMetadata,

View File

@ -6,7 +6,10 @@
*/
package org.hibernate.sql.results.jdbc.spi;
import java.util.Set;
import org.hibernate.Incubating;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.results.ResultSetMapping;
@ -31,4 +34,9 @@ public interface JdbcValuesMappingProducer {
JdbcValuesMapping resolve(
JdbcValuesMetadata jdbcResultsMetadata,
SessionFactoryImplementor sessionFactory);
default void addAffectedTableNames(Set<String> affectedTableNames, SessionFactoryImplementor sessionFactory) {
throw new NotYetImplementedFor6Exception( getClass() );
}
}

View File

@ -22,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*
* @author Hardy Ferentschik
*/
@FailureExpected( jiraKey = "HHH-4282" )
//@FailureExpected( jiraKey = "HHH-4282" )
@DomainModel(
annotatedClasses = HabitatSpeciesLink.class,
xmlMappings = "org/hibernate/orm/test/annotations/idclass/xml/HabitatSpeciesLink.xml"
@ -34,8 +34,8 @@ public class IdClassXmlTest {
scope.inTransaction(
session -> {
HabitatSpeciesLink link = new HabitatSpeciesLink();
link.setHabitatId( 1l );
link.setSpeciesId( 1l );
link.setHabitatId( 1L );
link.setSpeciesId( 1L );
session.persist( link );
Query q = session.getNamedQuery( "testQuery" );