如何使用hql将参数传递给@Query注解

How to pass parameter to @Query annotation with hql

这是我的 hql 请求:

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

我想 select 所有可见性为 true 的代理。

在我的 Agent class 中,有一个具有 getVisibility 和 setVisibility 函数的布尔可见性属性。在我的数据库中 "visibility" 存储为 bit(1).

我试过 a.visibility = 1, ... = '1', ...= 'TRUE', ...='true', ... 是真的。但是我得到这个错误:

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]

有什么建议吗?提前谢谢你。

将您的查询更改为:

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

在你的代码中,你必须写 table 的 Alice 名字,所以添加它。

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

您的查询不正确,您在trueorder by...之间多了一个a。所以正确的查询会变成这样

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

不确定这是否能解决您的所有问题。看看吧。