代码之家  ›  专栏  ›  技术社区  ›  SpringUser

组织。冬眠hql。内部的ast。QuerySyntaxException:意外的标记:在第1行第17列附近不同

  •  1
  • SpringUser  · 技术社区  · 7 年前

    我正在尝试使用Query方法在存储库中执行以下查询。我想要唯一的位置,结果应该是 JSON 格式(键、值)

    这是我的密码

    @Repository
    public interface AccountRepository extends JpaRepository<Account, Integer>, QueryDslPredicateExecutor<Account> {
    
        // Load location
        @Query("select new map (distinct(a.slocation) as slocation) from Account a where a.slocation !=null")
        Set<Account> findSlocation();
    

    错误

    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 17 [select new map (distinct(a.slocation) as slocation) from com.spacestudy.model.Account a where a.slocation !=null]
    

    有人能告诉我如何解决这个错误吗?

    2 回复  |  直到 7 年前
        1
  •  2
  •   M.A.Bell    7 年前

    您可以使用此选项:

    @Query("select new map (a.slocation) from Account a where a.slocation !=null group by slocation")
    
        2
  •  1
  •   SpringUser    7 年前

    我又得到了一个解决方案,我删除了distinct并添加了 Set 而不是 List

    @Query("select new map (a.slocation as slocation) from AccountModel a where a.slocation !=null")    
        Set<AccountModel> findBySlocation();