spring 数据 jpa:结果元组中未找到别名!确保您的查询定义了别名

spring data jpa: No aliases found in result tuple! Make sure your query defines aliases

当我尝试使用存储库界面获取用户时,我收到以下异常 "org.springframework.dao.InvalidDataAccessApiUsageException: No aliases found in result tuple! Make sure your query defines aliases!; nested exception is java.lang.IllegalStateException: No aliases found in result tuple! Make sure your query defines aliases!"

存储库:

@Repository
public interface UserRelationshipRepository
        extends JpaRepository<UserRelationship, Long>, QueryDslPredicateExecutor<UserRelationship> {

    @Query(value = "SELECT ur.id.toUser FROM UserRelationship ur WHERE ur.fromUser = :fromUser AND ur.relationshipTypeId = 1")
    Set<User> findUserFriends(@Param("fromUser") User fromUser);
}

实体:

@Entity
@NamedEntityGraph(name = "graph.User", attributeNodes = {})
@Table(name = "users")
public class User extends BaseEntity implements UserDetails {

    private static final long serialVersionUID = 8884184875433252086L;

    @Id
    @SequenceGenerator(name = "users_id_seq", sequenceName = "users_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "users_id_seq")
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fromUser", cascade = CascadeType.ALL)
    private Set<UserRelationship> relationships = new HashSet<UserRelationship>();
// getters setters
}

@Entity
@NamedEntityGraph(name = "graph.UserRelationship", attributeNodes = {})
@Table(name = "users_relationships")
public class UserRelationship extends BaseEntity implements Serializable {

    private static final long serialVersionUID = -6367981399229734837L;

    @EmbeddedId
    private final UserRelationshipId id = new UserRelationshipId();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "from_user_id", nullable = false)
    @MapsId("fromUserId") // maps fromUserId attribute of the embedded id
    private User fromUser;

    @Column(name = "relationship_type_id")
    private Long relationshipTypeId;

}

我正在使用 spring 数据 jpa 的“1.11.0.BUILD-SNAPSHOT”版本。 这是已知的 issue,它被标记为已解决,但我仍然明白。

请帮我解决这个问题。

更新: 如果我将存储库方法的 return 类型更改为 Set<Object> 那么一切正常。

您 运行 进入 DATAJPA-885,它已经修复并且将成为 Spring Data Hopper SR2 版本的一部分。