Skip to content

HHH-15497 Count query when counting polymorphic subclasses by type fails when run twice #5275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
protected QueryParameterBindingImpl(
QueryParameter<T> queryParameter,
SessionFactoryImplementor sessionFactory) {
this.queryParameter = queryParameter;
this.sessionFactory = sessionFactory;
this.bindType = queryParameter.getHibernateType();
this( queryParameter, sessionFactory, queryParameter.getHibernateType() );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
import org.hibernate.query.sqm.tree.domain.SqmPluralValuedSimplePath;
import org.hibernate.query.sqm.tree.domain.SqmTreatedPath;
import org.hibernate.query.sqm.tree.domain.SqmTreatedRoot;
import org.hibernate.query.sqm.tree.expression.AbstractSqmExpression;
import org.hibernate.query.sqm.tree.expression.Conversion;
import org.hibernate.query.sqm.tree.expression.JpaCriteriaParameter;
import org.hibernate.query.sqm.tree.expression.SqmAliasedNodeRef;
Expand Down Expand Up @@ -4890,11 +4891,8 @@ protected Expression consumeSqmParameter(
final QueryParameterImplementor<?> queryParameter = domainParameterXref.getQueryParameter( sqmParameter );
final QueryParameterBinding binding = domainParameterBindings.getBinding( queryParameter );
if ( binding.setType( valueMapping ) ) {
replaceJdbcParametersType(
sqmParameter,
domainParameterXref.getSqmParameters( queryParameter ),
valueMapping
);
// Align the SqmParameter expressible type with the binding type
( (AbstractSqmExpression) sqmParameter ).forceInferableType( (SqmExpressible<?>) binding.getBindType() );
}
return new SqmParameterInterpretation(
sqmParameter,
Expand Down Expand Up @@ -6802,6 +6800,8 @@ private Predicate processInSingleParameter(

if ( !iterator.hasNext() ) {
domainParamBinding.setType( (MappingModelExpressible) determineValueMapping( sqmPredicate.getTestExpression(), fromClauseIndex ) );
// Align the SqmParameter expressible type with the binding type
( (AbstractSqmExpression) sqmParameter ).forceInferableType( (SqmExpressible<?>) domainParamBinding.getBindType() );
return inListPredicate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,8 @@ public JpaSelection<T> alias(String name) {
public JavaType<T> getJavaTypeDescriptor() {
return getNodeType() == null ? null : getNodeType().getExpressibleJavaType();
}

public void forceInferableType(SqmExpressible<?> type){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move the logic from org.hibernate.query.internal.QueryParameterBindingImpl#setType to AbstractSqmParameter#applyInferableType and call that method instead. Changing the type of the parameter binding is a no-go AFAIU from our last discussion, so we shouldn't do it and deprecate the setType method.

setExpressibleType( type );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.hibernate.query.spi.QueryImplementor;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import jakarta.persistence.CascadeType;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
Expand All @@ -21,13 +33,6 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -56,7 +61,7 @@ public class SingleTableInheritancePersistTest {
private final List<Child> children = new ArrayList<>( Arrays.asList( susan, mark ) );
private final List<Person> familyMembers = Arrays.asList( john, jane, susan, mark );

@BeforeEach
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Expand All @@ -77,7 +82,6 @@ public void setUp(SessionFactoryScope scope) {

session.persist( family );
} );

}

@Test
Expand Down Expand Up @@ -109,6 +113,28 @@ else if ( person instanceof Child ) {
} );
}

@Test
@TestForIssue(jiraKey = "HHH-15497")
public void testFetchChildrenCountTwiceFails(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
QueryImplementor<Long> query = session.createQuery(
"SELECT count(p) FROM Person p WHERE TYPE(p) = ?1",
Long.class
);
query.setParameter( 1, Child.class );
Long personCount = query.getSingleResult();

assertThat( personCount, is( 2L ) );

query = session.createQuery( "SELECT count(p) FROM Person p WHERE TYPE(p) = ?1", Long.class );
query.setParameter( 1, Child.class );
personCount = query.getSingleResult();

assertThat( personCount, is( 2L ) );
} );
}

@Entity(name = "Family")
public static class Family {

Expand Down