如何构造Spring Data仓库查询带有IN和相同列表的两个参数?

时间:2019-03-21 06:28:01

标签: sql spring repository

这是我的实体:

@Data
@Entity
@IdClass(EtlJobExecutionTriggersId.class)
@Table(name = "ETL_JOB_EXEC_TRIGGERS")
public class EtlJobExecutionTriggers {
    @Id private Long jobExecIdUs;
    @Id private Long jobExecIdDs;
    private LocalDate cobDate;
}

这是复合主键类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
@EqualsAndHashCode
public class EtlJobExecutionTriggersId implements Serializable {
    private Long jobExecIdUs;
    private Long jobExecIdDs;
}

这是我的Spring Repo:

public interface EtlJobExecTriggersRepo extends JpaRepository<EtlJobExecutionTriggers, EtlJobExecutionTriggersId> {
        String SQL_ = "select o from EtlJobExecutionTriggers o where o.jobExecIdDs in (:ids) or o.jobExecIdUs in (:ids) order by o.jobExecIdUs, o.jobExecIdDs";
        @Query(EtlJobExecTriggersRepo.SQL_)
        List<EtlJobExecutionTriggers> findAllByJobExecIdDsInAndJobExecIdUsInSQL(@Param("ids") List<Long> jobExecIdList);
    }

@Query可以正常工作,但是我不想编写任何SQL,而是只使用Spring Data存储库查询来表达相同的查询。

我尝试了以下(和其他变体)

List<EtlJobExecutionTriggers> findAllByJobExecIdDsInAndJobExecIdUsInOrderByJobExecIdUsJobExecIdDs(List<Long> jobExecIdDsList)

但是启动时我总是收到错误消息。上面的接口方法为OrderBy部分产生以下异常:

org.springframework.data.mapping.PropertyReferenceException: No property jobExecIdDs found for type Long! Traversed path: EtlJobExecutionTriggers.jobExecIdUs.

那么我在这里做错了什么?还是无法通过Spring Data Repo查询来表达此特定查询?

正如我在评论中所写的那样,我修复了按问题排序,但是我仍然无法仅使用一个方法参数(List jobExecIdList

使它生效

当我使用两个(列表jobExecIdDsList,列表jobExecIdUsList

像这样:

List<EtlJobExecutionTriggers> findAllByJobExecIdDsInAndJobExecIdUsInOrderByJobExecIdUsAscJobExecIdDsAsc(List<Long> jobExecIdDsList, List<Long> jobExecIdUsList);

它实际上有效,但是我不能只使用一个列表,就像@Query(“ ....”)方法一样

2 个答案:

答案 0 :(得分:1)

//您应该在方法中有两个参数,如下所示。 列出findAllByJobExecIdDsInAndJobExecIdUsInOrderByJobExecIdUsJobExecIdDs(List jobExecIdDsList,List jobExecIdUsList);

答案 1 :(得分:0)

我认为使用您自己的自定义ID生成器与Spring Data Repository查询冲突。