HQL错误 - 加入期望的路径

时间:2017-06-27 08:00:08

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

我尝试并阅读了有关此问题的其他问题,但我无法将逻辑应用于我的案例。 我想从这张表中选择:

@Entity
public class LabelStatistics {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int ID;

    @Enumerated(EnumType.STRING)
    private AnalysisType type;

    private String labelId;
    private String hexLabelId;
    private Timestamp timestamp;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<LabelStatisticsItem> results;

我正在尝试执行以下语句:

@Query(value = "SELECT s1.labelId, s1.type, s1.timestamp "
        + "FROM LabelStatistics s1 "
        + "INNER JOIN LabelStatistics s2 on s1.labelId = s2.labelId  and s1.type = s2.type and s1.timestamp < s2.timestamp")
List<Object[]> findLatestStatisticsEntries();

我一直收到这个错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! 

有人可以解释一下如何解决这个问题吗? 最好的问候

1 个答案:

答案 0 :(得分:5)

我想这是因为只有在设置了关系的实体之间才允许连接。

尝试使用WHERE而不是

SELECT s1.labelId, s1.type, s1.timestamp  
FROM LabelStatistics s1, LabelStatistics s2
WHERE s1.labelId = s2.labelId  and s1.type = s2.type and s1.timestamp < s2.timestamp