I create custom query for getting Invoice data by Date, but it returns null. I'm sure that I set exactly the same Date for query that exists in database.
I use custom query because I want more advance query to write. But issue exist in this simple query. Here is my sample code:
@Query("select i from Invoice i where " +
"i.expirationDate = :expDate")
Invoice findCompanyInvoiceByDate(@Param("expDate") Date expDate);
I tried this code but it does not work also:
Invoice findByExpirationDate(Date expirationDate);
I also tried to add @Temporal(TemporalType.DATE)
before Date and @Param but result is null.
I just used the date as a String and used
nativeQuery = true
. It worked well for me.You should use
@Temporal(TemporalType.TIMESTAMP)
in your date column. If that still not enough (still return null), addcolumnDefinition
in@Column
annotation as well.Full working example is here (Note the so-40613171 branch. Sorry for weird repository name, and class naming. It uses by a lot of case study). Rough example:
Employee.java
EmployeeRepository.java
Sample Data
Test Code Snippet
If you run this, the test is passed.
HTH
I found the answer that was useful for this problem here.
If you use "date" type, I think the code from question should work, but for "datetime" you should use "between" in query or something similar. I found the solution that works for me, and this is the code: