From 73221d00903422fa22bf32b02a65cafd13ff7624 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Wed, 21 Feb 2024 14:23:16 +0100 Subject: [PATCH] HHH-17765 Support some special chars in query method queries --- .../annotation/AnnotationMetaEntity.java | 3 +- .../jpamodelgen/annotation/QueryMethod.java | 32 +++++++++++++++---- .../hibernate/jpamodelgen/test/dao/Dao.java | 3 ++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java index 7b0c55622c..b9d84bf8f2 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; +import java.util.regex.Pattern; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; @@ -1493,7 +1494,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { } private static boolean parameterIsMissing(String hql, int i, String param, String type) { - return !hql.matches(".*(:" + param + "|\\?" + i + ")\\b.*") + return !Pattern.compile( ".*(:" + param + "|\\?" + i + ")\\b.*", Pattern.DOTALL ).matcher( hql ).matches() && !isSessionParameter(type) && !isPageParam(type) && !isOrderParam(type); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java index 8fa7f972cc..1cca4ab1b7 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java @@ -241,13 +241,31 @@ public class QueryMethod extends AbstractQueryMethod { @Override public String getAttributeNameDeclarationString() { - return new StringBuilder() - .append("static final String ") - .append(getConstantName()) - .append(" = \"") - .append(queryString) - .append("\";") - .toString(); + StringBuilder sb = new StringBuilder(queryString.length() + 100) + .append( "static final String " ) + .append( getConstantName() ) + .append( " = \"" ); + for ( int i = 0; i < queryString.length(); i++ ) { + final char c = queryString.charAt( i ); + switch ( c ) { + case '\r': + sb.append( "\\r" ); + break; + case '\n': + sb.append( "\\n" ); + break; + case '\\': + sb.append( "\\\\" ); + break; + case '"': + sb.append( "\\\"" ); + break; + default: + sb.append( c ); + break; + } + } + return sb.append("\";").toString(); } private String getConstantName() { diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java index 56d95ff587..4198453873 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java @@ -112,4 +112,7 @@ public interface Dao { @HQL("select avg(pages) from Book") double averagePageCount(); + + @HQL("select b\nfrom Book b\nwhere b.isbn = :isbn") + Book findByIsbnMultiline(String isbn); }