Query annotation not working for max(id)
I have a domain object GenJournal and it has an "id" member (Long) that's auto-generated. I also have a JPA repository that I've added ... #Query("select coalesce(max(u.id), 0) from GenJournal u") Long getMaxId(); The method getMaxId() returns zero or null before I added coalesce. I have two rows in my database with ids 1 and 2. Can anyone help me determine why this doesn't work? I'm trying to get the latest or max id so that I can use the find method after to return the most recent GenJournal object in my service class. I'm stumped and really need some options or strategy to determine why this doesn't work.
You could use "Native Query" feature by passing nativeQuery = true param into #Query annotation like this #Query("select coalesce(max(u.id), 0) from Gen_Journal_Table u", NativeQuery = true) Long getMaxId();
My issue was two-fold. First I was getting null without the use of "coalesce". That caused me to think that this didn't work. When I adopted the use of "coalesce" I didn't realize that my table had no records and was returning the zero (0). My table in the production profile did have two records and I was expecting an id of 2. I was manually checking the wrong database and setting expectations that were incorrect.
Couchbase + Spring Data: Get only field instead of whole entity using #Query
How can I create a Predicate from a HQL query?
Left Join in Spring Data JPA's Specification
Lombok Usage - It doesn't serve its functionality in a unit test
Spring Data Couchbase findById always returns null
spring-data-neo4j repository with Camel fails to initialize
How does pages work if the DB is manipulated between next
How can I generate Q-classes with QueryDsl 4.1.4 and Spring-Data-Jpa 2.0.0.M1?
How to map a legacy database table with mismanaged attribute names
spring-data-redis: Unexpected end of stream exception
Is it possible to use OR and LIKE keyword together in Spring Data JPA
Save and find within same transaction
Why doesn't spring-data-elasticsearch use the objectmapper from the spring context
QueryDSL 4.3.1 : Generate JPA and SQL Types using Maven
How to write MongoTemplate Query and Criteria for a complex Document structure?
Custom Implementation AuditorAware in Spring Data