Hibernate查询 – 使用多个输入进行用户名模糊匹配

huangapple go评论82阅读模式
英文:

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: &#39;{test%,testuser6,%ana%}&#39; near line 1, column 108 [

SELECT jpauser FROM com.abc.domain.jpa.JpaUser jpauser WHERE  jpauser.username like  ANY(&#39;{test%,testuser6,%ana%}&#39;::text[]) limit 100]
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)

Code:

public List&lt;JpaUser&gt; getUserByCriteria(String criteria)
    {
    	Query query = getEntityManager()
                .createQuery(&quot;SELECT jpauser FROM JpaUser jpauser WHERE &quot; + criteria);
    	List&lt;JpaUser&gt; 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(&#39;{test%,testuser6,%ana%}&#39;::text[]) limit 100 

Construct the criteria like below

String value = criteria.get(&quot;username&quot;);
				builder.append(TABLE_ALIAS).append(&quot;username&quot;).append(&quot; like &quot;)
				.append(ANY).append(&quot;(&#39;{&quot;).append(value).append(&quot;}&#39;::text[])&quot;)

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(&quot;SELECT jpauser FROM JpaUser jpauser WHERE &quot; + 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(&quot;SELECT * FROM users jpauser WHERE &quot; + criteria)

huangapple
  • 本文由 发表于 2020年6月5日 22:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/62217510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定