如何替换 JPQL @Query 中的字符
How to replace characters in JPQL @Query
我的数据库有一个 table 资源,例如:
ID NAME
1 some_resource
2 another_resource
和部门
ID NAME RESOURCE_NAME
1 dep1 some-resource
2 dep2 another-resource
我需要能够在查询中通过资源名称匹配它们,为此我希望使用 SQL 服务器函数 REPLACE:
@Query("select res from Resource res where res.name in " +
"(select FUNCTION('REPLACE', dep.resourceName, '-', '_') from Department dep)")
Page<Resource> findByAll(Pageable pageable);
但是这会引发异常:
Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\-[METHOD_CALL] MethodNode: 'FUNCTION (REPLACE)'
+-[METHOD_NAME] IdentNode: 'REPLACE' {originalText=REPLACE}
您还需要注册 Hibernate 的标准SQL 函数,如下所示:
public class MySQLServerDialect extends SQLServerDialect {
public SQLServerDialect() {
super();
registerFunction("REPLACE", new StandardSQLFunction("REPLACE"));
}
}
我的数据库有一个 table 资源,例如:
ID NAME
1 some_resource
2 another_resource
和部门
ID NAME RESOURCE_NAME
1 dep1 some-resource
2 dep2 another-resource
我需要能够在查询中通过资源名称匹配它们,为此我希望使用 SQL 服务器函数 REPLACE:
@Query("select res from Resource res where res.name in " +
"(select FUNCTION('REPLACE', dep.resourceName, '-', '_') from Department dep)")
Page<Resource> findByAll(Pageable pageable);
但是这会引发异常:
Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\-[METHOD_CALL] MethodNode: 'FUNCTION (REPLACE)'
+-[METHOD_NAME] IdentNode: 'REPLACE' {originalText=REPLACE}
您还需要注册 Hibernate 的标准SQL 函数,如下所示:
public class MySQLServerDialect extends SQLServerDialect {
public SQLServerDialect() {
super();
registerFunction("REPLACE", new StandardSQLFunction("REPLACE"));
}
}