0

I have the following code:

    @Query("select t from Training t join t.skills s join t.trainers tr join t.discipline d  where " +
            "(t.name in :names or :names is null) and (s.name in :skills or :skills is null) and" +
            " (t.location = :location or :location is null) and " +
            " (d.name = :discipline or :discipline is null) and " +
            "(tr.firstName in :trainers or :trainers is null) and " +
            " (((:endDate > t.endDate) and (:startDate < t.startDate)) or (:startDate is empty))")
    public List<Training> filterTrainings(List<String> names, List<String> skills, String location,String discipline,List<String> trainers,Timestamp endDate,Timestamp startDate);

and i need to check if :startDate and :endDate are null. Is there a way to do that?

The error i get is nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

when trying to check :startDate is null where start date is a Timestamp.

6
  • Do you have to check before or after the join if the dates are null? Commented Sep 2, 2020 at 11:06
  • I don't think it matters for me.. Commented Sep 2, 2020 at 11:16
  • How is this different than your previous question? Commented Sep 2, 2020 at 11:20
  • 1
    Then what is your problem with :endDate IS NULL respectively endDate IS NOT NULL? Commented Sep 2, 2020 at 11:22
  • i get an error like "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet" Commented Sep 2, 2020 at 11:37

2 Answers 2

1

Could you try passing a LocalDateTime as a parameter instead of a Timestamp? java.sql.Timestamp might be causing you issues here. You can convert to a LocalDateTime by calling timestamp.toLocalDateTime()

Alternatively you could try passing the timestamp as a string into filterTrainings. If the timestamp is null before calling the filterTrainings method, assign an empty string. String _timestamp = timestamp == null ? "" : timestamp.toString()

Then, in your sql statement check if the string is empty .. ""=:timestamp OR function("to_timestamp", :timestamp, "yyyy-mm-dd hh:mm:ss.fffffffff"). The problem here is that we are using function to access native db commands.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you can build your SQL query with a and COALESCE(field, default_value_for_datetime_field). So you will not get NULL values.

You can build a default value as the start of the current month, of the current week, or any datetime value which could be a fair substitution for your model domain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.