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.
Spring Data JPA - Update Whole object
spring data load children and query by child property
What is the difference between all these spring DAO templates
Spring Batch and Neo4j: Transaction synchronization is not active
spring data not get id annotaion on method
spring jpa query dsl doesnt work with projection
Spring Elasticsearch data repository in Weld SE CDI - UnsatisfiedResolutionException
Spring Data query by example and Primefaces datatable filtering
spring-data-parent and spring-data-* projects with java 6?
SpringJPA + Eclipselink: No [ManagedType] was found for the key class. Not using persistance.xml
spring Batch partitioning + spring-data-mongo integration example
Trying to use Crate.io NoSql database with an existing Spring Data / Mysql project
How spring data generate sql according to method name?
Spring Data Jpa equality of two column
Spring Data Mongodb findBy In case insensitive
How to set arrays of string to #EnableJpaRepositories from property files