HHH-17765 Support some special chars in query method queries

This commit is contained in:
Christian Beikov 2024-02-21 14:23:16 +01:00
parent 647a1890e8
commit 73221d0090
3 changed files with 30 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Pattern;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element; 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) { 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) && !isSessionParameter(type)
&& !isPageParam(type) && !isPageParam(type)
&& !isOrderParam(type); && !isOrderParam(type);

View File

@ -241,13 +241,31 @@ public class QueryMethod extends AbstractQueryMethod {
@Override @Override
public String getAttributeNameDeclarationString() { public String getAttributeNameDeclarationString() {
return new StringBuilder() StringBuilder sb = new StringBuilder(queryString.length() + 100)
.append("static final String ") .append( "static final String " )
.append(getConstantName()) .append( getConstantName() )
.append(" = \"") .append( " = \"" );
.append(queryString) for ( int i = 0; i < queryString.length(); i++ ) {
.append("\";") final char c = queryString.charAt( i );
.toString(); 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() { private String getConstantName() {

View File

@ -112,4 +112,7 @@ public interface Dao {
@HQL("select avg(pages) from Book") @HQL("select avg(pages) from Book")
double averagePageCount(); double averagePageCount();
@HQL("select b\nfrom Book b\nwhere b.isbn = :isbn")
Book findByIsbnMultiline(String isbn);
} }