[英]Spring JPA entity findBy<Parameter> method giving values in 1st attempt and null in 2nd call?
[英]FindBy of spring JPA makes the thread hang when passing a null parameter
我有一个带有“Report”实体和一个“ReportRepository”的 spring jpa 应用程序,它扩展了 CrudRepository<Report, Long>。
在 ReportRepository 中,我实现了一种通过“reportNode”属性查找报告的方法,如下所示:
@Data
@Builder
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
public class Report {
@Id
@GeneratedValue(strategy = AUTO)
@EqualsAndHashCode.Include
private Long id;
private Long authorId;
private Long verifyingObserverId;
private LocalDateTime creationDate;
private LocalDateTime verificationDate;
private String title;
@Lob
@Column(length=2048)
private String description;
private boolean completed;
private boolean verified;
private String joinParameter;
private String reportNode;
@Lob
@Column(length=3060)
private String hl7;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "requested_procedure_id")
private RequestedProcedure requestedProcedure;
@OneToMany(mappedBy = "report",
cascade = CascadeType.ALL,
orphanRemoval = true)
private List<ReportVersion> reportVersions;
}
public interface ReportRepository extends CrudRepository<Report, Long> {
List<Report> findByReportNode(String reportNode);
}
当我尝试使用此方法传递非空参数时,它工作正常,返回找到的报告的列表,但是,当我传递空参数时,该方法永远不会结束它的执行。
为清楚起见,这有效:
List<Report> pendingReports = reportRepository.findByReportNode("");
但这永远不会返回:
List<Report> pendingReports = reportRepository.findByReportNode(null);
它也不会抛出任何异常或错误。
我想知道为什么会失败,以及如何使用 spring jpa 搜索表中具有空参数的所有实体。
提前致谢
编辑:
我尝试创建一个特定的方法来搜索空值,如下所示:
reportRepository.findByReportNodeIsNull()
但行为是一样的。 执行挂起。
如果要搜索节点为空的报告,则必须使用:
List<Report> pendingReports = reportRepository.findByReportNodeIsNull();
我最终找到了解决方案。 问题是“报告”类需要有一个没有参数的构造函数来让 jpa 创建它返回的对象。 如果你像我一样使用 lombok,你只需要添加以下注释:
@NoArgsConstructor
public class Report {
...
如此一来,双方
List<Report> pendingReports = reportRepository.findByReportNode(null);
和
List<Report> pendingReports = reportRepository.findByReportNodeIsNull()
按预期工作。
最后一点,我觉得很奇怪,当它尝试使用构造函数但没有找到构造函数时没有抛出异常,因为它使这个问题变得非常不清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.