我正在将mysql与jpa规范查询一起使用。 我想知道如何调用使用mysql关键字作为参数的函数。
这里是示例:
select * from schema3.countries order by convert(name using GBK);
使用convert
和using
关键字作为参数的GBK
方法。
我想通过条件查询来调用convert
函数。
我尝试了以下操作,但对我不起作用。
Expression expression = join.get(Country_.NAME);
Expression orderExpression = builder.function(
"convert",String.class,expression,builder.literal("USING GBK")
);
和
Path path = join.get(Country_.NAME);
String countryNameAlias = path.getalias();
Expression orderExpression = builder.function(
"convert",builder.literal(countryNameAlias + " USING GBK")
);
变量countryNameAlias
为空,因此它不起作用。
这是错误:
Hibernate: select expert0_.id as id1_14_,expert0_.code as code2_14_,expert0_.created_at as created_3_14_,expert0_.expert_information as expert_i4_14_,expert0_.meta_data_of_the_expert_information as meta_dat5_14_,expert0_.motherland as motherla8_14_,expert0_.number_of_applications as number_o6_14_,expert0_.updated_at as updated_7_14_,JSON_EXTRact(expert0_.expert_information,'$.basicInformation.birthDate') as formula4_,case
when
JSON_EXTRact(expert0_.expert_information,'$.basicInformation.gender') = 'MALE'
then 0
else 1 end as formula5_,'$.basicInformation.nameEN') as formula6_,convert(JSON_EXTRact(expert0_.expert_information,'$.basicInformation.nameZH') using GBK) as formula7_ from expert expert0_ left outer join expert_application_record expertappl1_ on expert0_.id=expertappl1_.expert_id left outer join countries country2_ on expert0_.motherland=country2_.id where expertappl1_.latest=? order by convert(?) desc limit ?
2019-11-05 18:58:41.281 TRACE 15252 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BOOLEAN] - [true]
2019-11-05 18:58:41.281 TRACE 15252 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [null USING GBK]
2019-11-05 18:58:41.282 WARN 15252 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064,SQLState: 42000
2019-11-05 18:58:41.282 ERROR 15252 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') desc limit 10' at line 5
2019-11-05 18:58:41.285 ERROR 15252 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataaccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') desc limit 10' at line 5
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1019) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-2.7.9.jar:na]
谢谢大家。