spring data jpa:在结果元组中找不到别名!确保您的查询定义了别名

时间:2016-06-06 13:21:11

标签: spring spring-data spring-data-jpa

当我尝试让用户使用存储库接口时,我收到以下异常 “org.springframework.dao.InvalidDataAccessApiUsageException:在结果元组中找不到别名!确保您的查询定义了别名!;嵌套异常是java.lang.IllegalStateException:在结果元组中找不到别名!确保您的查询定义了别名!“

存储库:

@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;

}

我正在使用' 1.11.0.BUILD-SNAPSHOT '版本的spring数据jpa。 这已经知道了issue,它被标记为已解决,但我仍然可以得到它。

请帮我解决这个问题。

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

1 个答案:

答案 0 :(得分:0)

您已经遇到DATAJPA-885,它已经修复,将成为Spring Data Hopper SR2版本的一部分。