[JIRA] (HHH-16419) Regression on calling postgresql functions via CriteriaBuilder
by 周雁鸣 (JIRA)
周雁鸣 ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNWNlNGNjOGMy... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16419?atlOrigin=eyJpIjoiNWNlNG... ) HHH-16419 ( https://hibernate.atlassian.net/browse/HHH-16419?atlOrigin=eyJpIjoiNWNlNG... ) Regression on calling postgresql functions via CriteriaBuilder ( https://hibernate.atlassian.net/browse/HHH-16419?atlOrigin=eyJpIjoiNWNlNG... )
Change By: 周雁鸣 ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
{code:java} public static <T> Predicate contains(Root<T> root, CriteriaBuilder cb, String propertyName, String item) {
Assert.doesNotContain(item, ",", "'item' should not contains comma");
Dialect dialect = getDialect(cb);
if (dialect instanceof MySQLDialect) {
return cb.greaterThan(cb.function("find_in_set", Integer.class, cb.literal(item), root.get(propertyName)),
0);
}
if (dialect instanceof PostgreSQLDialect) {
return cb.equal(cb.literal(item), cb.function("any", String.class,
cb.function("string_to_array", String[].class, root.get(propertyName), cb.literal(","))));
}
return cb.like(cb.concat(cb.concat(",", root.get(propertyName)), ","), '%' + item + '%');
}{code}
This method will generate sql for PostgresQL like:
{code:sql}select count(t1_0.id) from test_entity t1_0 where 'name0'=any(string_to_array(t1_0.names,',')){code}
It works fine with hibernate 5.x, but failed with 6.x, It comes from two issues.
# {{cb.function("string_to_array", String[].class, ... )}} will throw {{java.lang.IllegalArgumentException: Passed `invariantType` for function return cannot be null}}, because {{SqmCriteriaNodeBuilder::function}} doesn’t recognize array type {{String[].class}}, It may resolved by
{code:java} final BasicType<T> resultType;
if (type.isArray()) {
BasicType<?> componentType = getTypeConfiguration().getBasicTypeForJavaType(type.getComponentType());
resultType = new BasicArrayType(componentType, new ArrayJdbcType( componentType.getJdbcType() ) ,
new ArrayJavaType<>( componentType.getJdbcJavaType()) ) ;
}
else {
resultType = getTypeConfiguration().getBasicTypeForJavaType(type);
}{code}
# {{cb.function("any", ... )}} will throw {{org.hibernate.QueryException: Parameter 1 of function bool_or() has type BOOLEAN, but argument is of type java.lang.String}}, It seems conflict with built-in function from {{CommonFunctionFactory}}
( https://hibernate.atlassian.net/browse/HHH-16419#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16419#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100221- sha1:d541bce )
1 year, 1 month
[JIRA] (HHH-16419) Regression on calling postgresql functions via CriteriaBuilder
by 周雁鸣 (JIRA)
周雁鸣 ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYTE0MDRkNGJk... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16419?atlOrigin=eyJpIjoiYTE0MD... ) HHH-16419 ( https://hibernate.atlassian.net/browse/HHH-16419?atlOrigin=eyJpIjoiYTE0MD... ) Regression on calling postgresql functions via CriteriaBuilder ( https://hibernate.atlassian.net/browse/HHH-16419?atlOrigin=eyJpIjoiYTE0MD... )
Issue Type: Bug Affects Versions: 6.1.7 Assignee: Unassigned Components: hibernate-core Created: 02/Apr/2023 22:57 PM Priority: Major Reporter: 周雁鸣 ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
public static <T> Predicate contains(Root<T> root, CriteriaBuilder cb, String propertyName, String item) {
Assert.doesNotContain(item, "," , " 'item' should not contains comma" );
Dialect dialect = getDialect(cb);
if (dialect instanceof MySQLDialect) {
return cb.greaterThan(cb.function( "find_in_set" , Integer.class, cb.literal(item), root.get(propertyName)),
0);
}
if (dialect instanceof PostgreSQLDialect) {
return cb.equal(cb.literal(item), cb.function( "any" , String.class,
cb.function( "string_to_array" , String [].class, root.get(propertyName), cb.literal( "," ))));
}
return cb.like(cb.concat(cb.concat( "," , root.get(propertyName)), "," ), '%' + item + '%' );
}
This method will generate sql for PostgresQL like:
select count (t1_0.id) from test_entity t1_0 where 'name0' = any (string_to_array(t1_0. names , ',' ))
It works fine with hibernate 5.x, but failed with 6.x, It comes from two issues.
* cb.function("string_to_array", String[].class, ... ) will throw java.lang.IllegalArgumentException: Passed `invariantType` for function return cannot be null , because SqmCriteriaNodeBuilder::function doesn’t recognize array type String[].class , It may resolved by
final BasicType<T> resultType;
if (type.isArray()) {
BasicType<?> componentType = getTypeConfiguration().getBasicTypeForJavaType(type.getComponentType());
resultType = new BasicArrayType(componentType, componentType.getJdbcType(),
componentType.getJdbcJavaType());
}
else {
resultType = getTypeConfiguration().getBasicTypeForJavaType(type);
}
* cb.function("any", ... ) will throw org.hibernate.QueryException: Parameter 1 of function bool_or() has type BOOLEAN, but argument is of type java.lang.String , It seems conflict with built-in function from CommonFunctionFactory
( https://hibernate.atlassian.net/browse/HHH-16419#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16419#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100221- sha1:d541bce )
1 year, 1 month