【发布时间】:2020-01-18 13:05:03
【问题描述】:
我有两个相同的查询。一个由entityManager.createQuery() 运行,另一个作为PagingAndSortingRepository 中的@Query 注释运行。 entityManager 查询运行良好,但 @Query 注解中的相同查询返回错误。
这可能是因为 @Query 注释将查询作为 JPQL 执行,而 entityManager.createQuery() 将查询作为 HQL 执行?
这里有两个例子:
@Query(不起作用)
@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()));
}
【问题讨论】:
-
我删除并重新创建了数据库,它开始正常工作。
标签: java hibernate spring-data-jpa spring-data jpql