我有兩個(gè)相同的查詢。一個(gè)正在運(yùn)行,entityManager.createQuery()另一個(gè)@Query作為PagingAndSortingRepository. entityManager 查詢運(yùn)行正常,但 @Query 注釋中的相同查詢返回錯(cuò)誤??赡苁且?yàn)锧Query注釋將查詢作為 JPQL 執(zhí)行,而將entityManager.createQuery()查詢作為 HQL 執(zhí)行?這是兩個(gè)例子:@Query(不起作用)@Repositorypublic interface UserRepository extends PagingAndSortingRepository<User, UUID> { @Query("select p.user from Perspective p where p.organisation.id = 'c25c86a0-0d8e-4beb-9ba7-e38d932b8410'") List<User> findUsers();}could not resolve property: lastname of: org.jembi.appstore.service.entities.Perspective// note: lastname is a property of user, not perspective.entityManager.createQuery(確實(shí)有效) @Autowired EntityManager entityManager; @RequestMapping("/query") @ResponseBody public void testQuery() { Query query = entityManager.createQuery("select p.user from Perspective p where p.organisation.id = 'c25c86a0-0d8e-4beb-9ba7-e38d932b8410'"); List<User> users = query.getResultList(); users.forEach(u -> System.out.println(u.getFirstname())); }
2 回答
UYOU
TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
這里有幾個(gè)錯(cuò)誤:
您正在定義
@Param("organisationId"),但不在任何地方使用它。您正在加入 user:
join User u,但從不使用它:與定義沒有任何關(guān)系Perspective,也沒有在where子句中使用。
嘗試這個(gè)查詢:
@Query("select p.user from Perspective p where p.organisation.id = :organisationId")
List<User> findUsers(@Param("organisationId") UUID organisationId, Pageable pageable);
一只萌萌小番薯
TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
@Query("select p.user from Perspective p join User u where p.organisation.id = 'c25c86a0-0d8e-4beb-9ba7-e38d932b8410'")
List<User> findUsers(@Param("organisationId") UUID organisationId, Pageable pageable);
}我建議你將其重寫為:
@Query("select p.user from Perspective p join User u where p.organisation.id = ?1")
List<User> findUsers(String organisationId);
}為什么String又不UUID呢?我敢打賭 HQL 不適用于 UUID。+ UUID 可以放入字符串中。
為什么我刪除了 Pageable?我在您的查詢中找不到您使用第二個(gè)參數(shù) [Pageable] 的位置。
添加回答
舉報(bào)
0/150
提交
取消
