0

In the below I can have the modifedDate as null some times. at that time it is throwing error as "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"

How can I get data even the modifiedDate is null. Some time the date will be coming. How do this ?

I want the below Query method return data always

  1. return based on adminStatus if modifiedDate is null.
  2. return based on adminStatus and modifiedDate. if modifiedDate is not null.

we can do by building a dynamic query and executing with entity Manager. but I want with repository.

@Repository
public interface AdminRepository extends JpaRepository<AdminEntity, Long> {

@Query(
     value="SELECT * from admin_entity where status=:adminStatus and modified_tsz>=:modifedDate:", 
    nativeQuery=true)
  List<AdminEntity> getAdmins(String adminStatus, Date modifedDate)
}


Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
saij9999
  • 282
  • 1
  • 3
  • 18
  • Try this, https://stackoverflow.com/questions/32728843/spring-data-optional-parameter-in-query-method – Vishal V Sep 29 '20 at 07:56
  • I have answered a similar question here https://stackoverflow.com/questions/58240024/search-by-many-optional-parameters-in-spring-data-jpa-repository/58243100#58243100 – Oleksii Valuiskyi Sep 29 '20 at 12:16

1 Answers1

0

Try this

@Repository
public interface AdminRepository extends JpaRepository<AdminEntity, Long> {

@Query(
     value="SELECT * from admin_entity where status=:adminStatus
           AND  (modified_tsz>=:modifedDate OR :modifedDate is null)", 
    nativeQuery=true)
  List<AdminEntity> getAdmins(String adminStatus, Date modifedDate)
}
Wildfire
  • 162
  • 2
  • 16
  • after this , i have got the another error. database is postgres with dates as timestamp with timezone. error operator does not exist timestamp with time zone >= bytea this is the error. – saij9999 Sep 29 '20 at 10:10
  • The error now relies on the datatype of your date. Does your previous code work when modifedDate isn't null ? – Wildfire Sep 29 '20 at 11:13
  • i have changed code: this is the error: ***** org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to timestamp with time zone.*** ******CHANGED CODE: + " AND (cast(:modifiedDate as timestamptz ) IS NULL OR ie.modified_tsz >= :modifiedDate)" – saij9999 Sep 29 '20 at 11:57