如何使用Join定义JPA存储库查询?

我想通过注释@Query通过Jpa存储库创建一个Join查询我有三个表。

原生查询是:

select application.APP_ID from user, customer, application where user.USE_CUSTOMER_ID = customer.CUS_ID and application.APP_CUSTOMER_ID = customer.CUS_ID and user.USE_ID=1; 

现在我有了Table Hibernate实体,所以我在ApplicationRepository中尝试过

 @Query(SELECT application FROM Application a INNER JOIN customer c ON c.customer.id = a.customer.id INNER JOIN user u ON u.customer.id = c.customer.id INNER JOIN application a ON a.user.id = u.id WHERE u.id = :user.id) List findApplicationsByUser(@Param("User") User user); 

日志说

意外的标记

请问有什么想法吗?

我的表实体

Application.java:

 @Entity @Table public class Application extends BaseSimpleEntity { ... @ManyToOne(optional = false) private Customer customer; ... } 

Customer.java:

 @Entity @Table public class Customer extends BaseSimpleEntity { ... @OneToMany(mappedBy = "customer") private List users; @OneToMany(mappedBy = "customer") private List applications; ... } 

User.java:

 @Entity @Table public class User extends BaseSimpleEntity { ... @ManyToOne(optional = false) private Customer customer; ... } 

JPA中不需要ON子句,因为JPA已经知道实体是如何关联的,这要归功于映射注释。

此外,您正在选择application ,该application不是查询中定义的别名。

你的联盟毫无意义。

查询应该只是

 select application FROM Application a join a.customer c join c.users u where u.id = :userId 

阅读Hibernate文档以了解HQL和联接的工作原理。