How to pass parameter to @Query annotation with hq

2019-03-06 17:37发布

问题:

Here is my hql requete :

@Query("select a from Agent where a.visibility = true a order by a.id desc")
public Page<Agent> getAllAgents(Pageable pageable);

I want to select all agents that have visibility true.

In my Agent class a have Boolean visibility attribute with getVisibility and setVisibility functions. In my data base "visibility" stored as bit(1).

I tried a.visibility = 1, ... = '1', ...= 'TRUE', ...='true', ... is true. But i get this error :

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: a near line 1, column 74 [select a from com.GemCrmTickets.entities.Agent where a.visibility = true a order by a.id desc]

Any suggestions ? Thank you in advance.

回答1:

Your query is not correct, you have an extra a between true and order by.... So the correct query would become like this

select a from Agent a where a.visibility = true order by a.id desc

Not sure if that fixes all your troubles. Check it out.



回答2:

Change your query to this:

@Query("select a from Agent a where a.visibility = true order by a.id desc")
public Page<Agent> getAllAgents(Pageable pageable);


回答3:

In your code, you have to write the Alice name of the table so add it.

@Query("select a from Agent a where a.visibility = true order by a.id desc")