我可以将 HQL 与 JPA 的 @Query 注释一起使用吗?
Can I use HQL with JPA's @Query annotation?
我有两个相同的查询。一个被 entityManager.createQuery()
运行,另一个作为 PagingAndSortingRepository
中的 @Query
注释。 entityManager 查询 运行 没问题,但 @Query 注释中的相同查询 returns 出错。
可能是因为 @Query
注释以 JPQL 执行查询而 entityManager.createQuery()
以 HQL 执行查询?
下面是两个例子:
@查询(无效)
@Repository
public 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(有效)
@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()));
}
@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 可以放入String.
为什么我删除了 Pageable?我在你的查询中找不到你在哪里使用第二个参数 [Pageable]。
这里有几个错误:
- 您正在定义
@Param("organisationId")
,但没有在任何地方使用它。
- 您正在加入用户:
join User u
,但永远不要使用它:既没有定义与 Perspective
的任何关系,也没有在 where
子句中使用它。
试试这个查询:
@Query("select p.user from Perspective p where p.organisation.id = :organisationId")
List<User> findUsers(@Param("organisationId") UUID organisationId, Pageable pageable);
我有两个相同的查询。一个被 entityManager.createQuery()
运行,另一个作为 PagingAndSortingRepository
中的 @Query
注释。 entityManager 查询 运行 没问题,但 @Query 注释中的相同查询 returns 出错。
可能是因为 @Query
注释以 JPQL 执行查询而 entityManager.createQuery()
以 HQL 执行查询?
下面是两个例子:
@查询(无效)
@Repository
public 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(有效)
@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()));
}
@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 可以放入String.
为什么我删除了 Pageable?我在你的查询中找不到你在哪里使用第二个参数 [Pageable]。
这里有几个错误:
- 您正在定义
@Param("organisationId")
,但没有在任何地方使用它。 - 您正在加入用户:
join User u
,但永远不要使用它:既没有定义与Perspective
的任何关系,也没有在where
子句中使用它。
试试这个查询:
@Query("select p.user from Perspective p where p.organisation.id = :organisationId")
List<User> findUsers(@Param("organisationId") UUID organisationId, Pageable pageable);