在 spring 数据 jpa 中使用两个不同的实体加入

Join using two different entities in spring data jpa

我有 2 个不同的实体。 table1 有一列是 table2 的主键。

两个表都有相关的存储库。

如果我在其中一个存储库中编写以下查询,它会给出错误

QuerySyntaxException: unexpected token:

@Query("select new stats.UserCountDTO(b.objectiveId, count(b.objectiveId), a.locationCountry)"+ 
" from UserIdentityEntity a, UserObjectiveEntity b where b.userIdentityId == a.id and b.cId = ?1")

在这种情况下如何使用 spring 数据 jpa 编写连接查询?

你只是犯了一个错误。

b.userIdentityId == a.id 

应该是:

b.userIdentityId = a.id