【问题标题】:Spring JPA. How to get only a list of IDs using @Query春季 JPA。如何使用@Query 仅获取 ID 列表
【发布时间】:2020-08-07 10:49:08
【问题描述】:

我正在尝试写这个来获取列表,我必须将 ID 列表作为参数传递:

@Query(value = "SELECT OutbreakDiagnosticTests FROM OutbreakDiagnosticTests WHERE OutbreakDiagnosticTests.outbreaks  in (:outbreakIds) ")
List<OutbreakDiagnosticTests> getDiagnosticTestsByOutbreaks(@Param("outbreakIds") List<Long> outbreakIds);

我的实体是这个,我用来查询的是这个:

@Entity
@EntityListeners(OutbreakDiagnosticTestManagerImpl.class)
@Table(name = "outbreak_diagnostic_tests")
public class OutbreakDiagnosticTests extends AbstractTemporalWorkingData implements Serializable{
  
/**
   * 
   */
  private static final long serialVersionUID = 636298998880960358L;

  @Id
  @Column(nullable = false, name = "obdt_id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long obdtId;

  @Column(nullable = false, name = "nature", length = 255)
  private String nature;

  @Column(nullable = true, name = "diagnostic_test_req", length = 255)
  private String diagnosticTestReq;

  @OneToMany(mappedBy = "outbreakDiagnosticTests", cascade = CascadeType.ALL, orphanRemoval = true)
  @Filter(name = "workingData")
  private Set<TestsResults> testsResults;

  @ManyToOne
  @JoinColumn(name = "outbreak_id", nullable = false)
  private Outbreaks outbreaks;

  @ManyToOne
  @JoinColumn(name = "diag_test_id", nullable = true)
  private DiagnosticTests diagTests;

  @ManyToOne
  @JoinColumn(name = "lab_id", nullable = true)
  private Laboratories lab;

  @Column(nullable = false, name = "is_field")
  private Boolean isField = false;

但是运行后我得到了这个异常:

Error creating bean with name 'labTestSummariesDaoImpl': Unsatisfied dependency expressed through field 'obDiagTestRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'outbreakDiagnosticTestRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.wahisplus.wcommon.repository.outbreak.OutbreakDiagnosticTestRepository.getDiagnosticTestsByOutbreaks(java.util.List)!

有人可以帮助我使用上述方法获取列表,或者可以告诉我我在这方面做错了什么。我不想使用本机即(nativeQuery = true)使用它。

【问题讨论】:

    标签: spring-boot hibernate jpa spring-data-jpa


    【解决方案1】:

    使用 OutbreakDiagnosticTests o 之类的别名,对于选择 id,使用 o.outbreaks.id 并确保您已正确自动连接存储库

    @Query(value = "SELECT o FROM OutbreakDiagnosticTests o WHERE o.outbreaks.id IN (:outbreakIds) ")
    List<OutbreakDiagnosticTests> getDiagnosticTestsByOutbreaks(@Param("outbreakIds") List<Long> outbreakIds);
    

    【讨论】:

      【解决方案2】:

      由于您已经在使用 Spring Data JPA,因此提供了一种方法 OOTB: 列出 findAllById(Iterable ids);

      此方法接收一个非空 ID 的列表,并根据这些 ID 返回记录。既然您没有使用 DTO 概念并为一组 id 获取所有列,为什么不使用此方法。

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 2021-06-17
        • 1970-01-01
        • 2015-02-27
        • 2020-09-02
        相关资源
        最近更新 更多