spring-data


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.

Related Links

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

Categories

HOME
opencart
tomcat
transactions
compilation
out-of-memory
visual-studio-2010
apache-kafka-connect
supercollider
u-boot
telegraf
timber
yeoman-generator-angular
watch-os-3
cname
mailmerge
google-awareness
heap-dump
nest-api
generator
tracing
unrar
fstar
spreadsheetgear
offline-caching
django-import-export
credit-card
dotcms
opnet
actframework
appcode
serversocket
payeezy
love2d
django-crispy-forms
django-smart-selects
praat
netapp
newtons-method
fluid-mac-app-engine
sendinput
nuclio
react-intl
axis
autorelease
phppgadmin
visual-format-language
simple-schema
lvalue
kendo-treeview
oci
android-4.2-jelly-bean
gravatar
univocity
wepay
android-mapview
juniper-network-connect
fanotify
srv-record
search-form
supersocket.net
compositetype
physicsjs
tree-structure
ocmockito
fuseesb
tkx
n-tier-architecture
cout
inputbox
android-2.3-gingerbread
network-printers
miglayout
meteor-collections
boolean-algebra
particle-swarm
ofstream
icenium
email-verification
struts2-json-plugin
audiotoolbox
sfinae
jdom
type-safety
multidrop-bus
delphi-prism
downgrade
bass
jquery-effects
rakudo
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App