我想通过注释@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<Application> 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<User> users; @OneToMany(mappedBy = "customer") private List<Application> 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和联接的工作方式。