英文:
Hibernate query - username like with multiple inputs
问题
我尝试按照条件获取用户,但出现以下错误:
错误:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ''{test%,testuser6,%ana%}' near line 1, column 108 [
SELECT jpauser FROM com.abc.domain.jpa.JpaUser jpauser WHERE jpauser.username like ANY('test%,testuser6,%ana%'::text[]) limit 100]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
代码:
public List<JpaUser> getUserByCriteria(String criteria)
{
Query query = getEntityManager()
.createQuery("SELECT jpauser FROM JpaUser jpauser WHERE " + criteria);
List<JpaUser> result = query.getResultList();
return result;
}
我只是复制并在 PostgreSQL 终端中执行了查询,它可以正常工作,但通过 Hibernate 查询会失败。
以下是我在 PostgreSQL 中执行的查询:
SELECT * FROM users jpauser WHERE jpauser.username like ANY('{test%,testuser6,%ana%}'::text[]) limit 100
构建条件的方式如下:
String value = criteria.get("username");
builder.append(TABLE_ALIAS).append("username").append(" like ")
.append(ANY).append("('{").append(value).append("}'::text[])")
有人能帮忙解决这个问题吗,如何执行带有 like 和多个输入的 Hibernate 查询?
英文:
i am tried to get user by criteria, but getting error like below
Error:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: '{test%,testuser6,%ana%}' near line 1, column 108 [
SELECT jpauser FROM com.abc.domain.jpa.JpaUser jpauser WHERE jpauser.username like ANY('{test%,testuser6,%ana%}'::text[]) limit 100]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
Code:
public List<JpaUser> getUserByCriteria(String criteria)
{
Query query = getEntityManager()
.createQuery("SELECT jpauser FROM JpaUser jpauser WHERE " + criteria);
List<JpaUser> result = query.getResultList();
return result;
}
I just copied and executed the query in PostgreSQL terminal, its works fine, but through hibernate query its failed
Here is the query i have executed in PostgreSQL
SELECT * FROM users jpauser WHERE jpauser.username like ANY('{test%,testuser6,%ana%}'::text[]) limit 100
Construct the criteria like below
String value = criteria.get("username");
builder.append(TABLE_ALIAS).append("username").append(" like ")
.append(ANY).append("('{").append(value).append("}'::text[])")
Can any one help on this, how to execute hibernate query with like and multiple input ?
答案1
得分: 1
使用createNativeQuery
来进行原生查询
Query query = getEntityManager()
.createNativeQuery("SELECT jpauser FROM JpaUser jpauser WHERE " + criteria);
英文:
Use createNativeQuery
for native query
Query query = getEntityManager()
.createNativeQuery("SELECT jpauser FROM JpaUser jpauser WHERE " + criteria);
答案2
得分: 0
在本地查询中,您需要使用本地SQL语句:
Query query = getEntityManager()
.createNativeQuery("SELECT * FROM users jpauser WHERE " + criteria);
英文:
In a native query, You need to use native SQL statment:
Query query = getEntityManager()
.createNativeQuery("SELECT * FROM users jpauser WHERE " + criteria)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论