0

I have a spring jpa application with a "Report" entity and a "ReportRepository" which extends CrudRepository<Report, Long>.

In the ReportRepository I've implemented a method to find Reports by it's "reportNode" attribute like so:

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

When I try to use this method passing a non null parameter it works fine, returning a list of the found Reports, but, when I pass a null parameter, the method never ends it's execution.

For clarity, this works:

    List<Report> pendingReports = reportRepository.findByReportNode("");

But this never returns:

    List<Report> pendingReports = reportRepository.findByReportNode(null);

It doesn't throw any exception or error either.

I would like to know why this fails and how can I search all entities in a table which have a null parameter with spring jpa.

Thanks in advance

EDIT:

I've tried creating a specific method to search null values like so:

reportRepository.findByReportNodeIsNull()

But the behavior is the same. The execution hangs.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Gatocan
  • 77
  • 1
  • 2
  • 7
  • maybe duplicate of https://stackoverflow.com/questions/30219486/spring-data-jpa-and-parameters-that-can-be-null – bubbles Jun 26 '19 at 12:00
  • @ElmaCherb I don't think it is. In that question it looks like they are doing the same I do, but it works for them while it hangs execution in my code. – Gatocan Jun 27 '19 at 11:49

2 Answers2

0

If you want to search for reports where the node is null you have to use:

List<Report> pendingReports = reportRepository.findByReportNodeIsNull();
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

I ended up finding the solution. The problem was that the "Report" class needs to have a constructor with no arguments for jpa to create the objects it returns. If you use lombok like I do, you just need to add the following anotation:

@NoArgsConstructor
public class Report {
...

With that, both

List<Report> pendingReports = reportRepository.findByReportNode(null);

and

List<Report> pendingReports = reportRepository.findByReportNodeIsNull()

work as intended.

As a final note, I find it weird that no exception is thrown when it tries to use a constructor and doesn't find one, as it makes this problem really unclear.

Gatocan
  • 77
  • 1
  • 2
  • 7