HHH-15291 Fix translation issue with SqmCoalesce

This commit is contained in:
Christian Beikov 2022-05-23 11:47:38 +02:00
parent 5f11d4763b
commit f3789090bc
2 changed files with 186 additions and 0 deletions

View File

@ -109,6 +109,7 @@ import org.hibernate.query.QueryLogging;
import org.hibernate.query.ReturnableType; import org.hibernate.query.ReturnableType;
import org.hibernate.query.SemanticException; import org.hibernate.query.SemanticException;
import org.hibernate.query.criteria.JpaPath; import org.hibernate.query.criteria.JpaPath;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.spi.QueryParameterBinding; import org.hibernate.query.spi.QueryParameterBinding;
import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.query.spi.QueryParameterBindings;
@ -182,6 +183,7 @@ import org.hibernate.query.sqm.tree.expression.SqmByUnit;
import org.hibernate.query.sqm.tree.expression.SqmCaseSearched; import org.hibernate.query.sqm.tree.expression.SqmCaseSearched;
import org.hibernate.query.sqm.tree.expression.SqmCaseSimple; import org.hibernate.query.sqm.tree.expression.SqmCaseSimple;
import org.hibernate.query.sqm.tree.expression.SqmCastTarget; import org.hibernate.query.sqm.tree.expression.SqmCastTarget;
import org.hibernate.query.sqm.tree.expression.SqmCoalesce;
import org.hibernate.query.sqm.tree.expression.SqmCollation; import org.hibernate.query.sqm.tree.expression.SqmCollation;
import org.hibernate.query.sqm.tree.expression.SqmCollectionSize; import org.hibernate.query.sqm.tree.expression.SqmCollectionSize;
import org.hibernate.query.sqm.tree.expression.SqmDistinct; import org.hibernate.query.sqm.tree.expression.SqmDistinct;
@ -5194,6 +5196,20 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
return new Format( sqmFormat.getLiteralValue() ); return new Format( sqmFormat.getLiteralValue() );
} }
@Override
public Object visitCoalesce(SqmCoalesce<?> sqmCoalesce) {
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final QueryEngine queryEngine = sessionFactory.getQueryEngine();
return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor( "coalesce" )
.generateSqmExpression(
sqmCoalesce.getArguments(),
null,
queryEngine,
sessionFactory.getTypeConfiguration()
)
.accept( this );
}
@Override @Override
public Object visitUnaryOperationExpression(SqmUnaryOperation<?> expression) { public Object visitUnaryOperationExpression(SqmUnaryOperation<?> expression) {
return new UnaryOperation( return new UnaryOperation(

View File

@ -0,0 +1,170 @@
/*
* 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.orm.test.jpa.criteria;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Root;
/**
* @author Will Dazy
*/
@Jpa(annotatedClasses = CoalesceTest.HHH15291Entity.class)
@TestForIssue( jiraKey = "HHH-15291")
public class CoalesceTest {
@Test
public void hhh15291JPQL1Test(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
TypedQuery<HHH15291Entity> query = entityManager.createQuery(
"" + "SELECT t FROM HHH15291Entity t "
+ "WHERE t.itemString2 = " + "COALESCE (t.itemString1, ?1)",
HHH15291Entity.class
);
query.setParameter( 1, "Sample" );
query.getResultList();
}
);
}
@Test
public void hhh15291JPQL2Test(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
TypedQuery<String> query2 = entityManager.createQuery(
"" + "SELECT COALESCE (t.itemString2, ?1) FROM HHH15291Entity t ORDER BY t.itemInteger1 ASC",
String.class
);
query2.setParameter( 1, "Sample" );
}
);
}
@Test
public void hhh15291Criteria1Test(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<HHH15291Entity> cquery = cb.createQuery( HHH15291Entity.class );
Root<HHH15291Entity> root = cquery.from( HHH15291Entity.class );
cquery.select( root );
ParameterExpression<String> checkParam1 = cb.parameter( String.class );
Expression<String> coalesce = cb.coalesce( root.get( "itemString1" ), checkParam1 );
cquery.where( cb.equal( root.get( "itemString2" ), coalesce ) );
TypedQuery<HHH15291Entity> query = entityManager.createQuery( cquery );
query.setParameter( checkParam1, "Sample" );
query.getResultList();
}
);
}
@Test
public void hhh15291Criteria2Test(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<HHH15291Entity> cquery = cb.createQuery( HHH15291Entity.class );
Root<HHH15291Entity> root = cquery.from( HHH15291Entity.class );
cquery.select( root );
ParameterExpression<String> checkParam1 = cb.parameter( String.class );
Expression<String> coalesce = cb.coalesce( root.get( "itemString1" ), checkParam1 );
cquery.where( cb.equal( root.get( "itemString2" ), coalesce ) );
TypedQuery<HHH15291Entity> query = entityManager.createQuery( cquery );
query.setParameter( checkParam1, "Sample" );
query.getResultList();
}
);
}
@Entity(name = "HHH15291Entity")
public static class HHH15291Entity {
@Id
@Column(name = "KEY_CHAR")
private String KeyString;
@Column(name = "ITEM_STRING1")
private String itemString1;
@Column(name = "ITEM_STRING2")
private String itemString2;
@Column(name = "ITEM_STRING3")
private String itemString3;
@Column(name = "ITEM_STRING4")
private String itemString4;
@Column(name = "ITEM_INTEGER1")
private Integer itemInteger1;
public String getKeyString() {
return KeyString;
}
public void setKeyString(String keyString) {
KeyString = keyString;
}
public String getItemString1() {
return itemString1;
}
public void setItemString1(String itemString1) {
this.itemString1 = itemString1;
}
public String getItemString2() {
return itemString2;
}
public void setItemString2(String itemString2) {
this.itemString2 = itemString2;
}
public String getItemString3() {
return itemString3;
}
public void setItemString3(String itemString3) {
this.itemString3 = itemString3;
}
public String getItemString4() {
return itemString4;
}
public void setItemString4(String itemString4) {
this.itemString4 = itemString4;
}
public Integer getItemInteger1() {
return itemInteger1;
}
public void setItemInteger1(Integer itemInteger1) {
this.itemInteger1 = itemInteger1;
}
}
}