首页 > 解决方案 > 在spring data jpa中使用两个不同的实体加入

问题描述

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

这两个表都尊重存储库。

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

QuerySyntaxException:意外令牌:

@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 data jpa 编写连接查询?

标签: spring-bootjpaspring-data-jpajpql

解决方案


你只是犯了一个错误。这

b.userIdentityId == a.id 

应该:

b.userIdentityId = a.id

推荐阅读