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

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

Categories

HOME
entity-framework
extjs
protocol-buffers
openxml
mirc
google-webmaster-tools
azure-mobile-services
stored-procedures
graphql-js
anchor
selection
port
yeoman-generator-angular
nexus3
erd
smooks
jquery-file-upload
fosuserbundle
adobe-illustrator
amazon-mws
android-permissions
backtracking
batch-rename
mod-wsgi
opentext
nurbs
webviewclient
basex
boolean-logic
splunk-query
publishing
snap-framework
cloveretl
spring-rabbitmq
buildforge
pdfminer
stackpanel
spring-restcontroller
export-to-pdf
python-jira
opal-framework
contactless-smartcard
spooler
oracle-bmcs
vesta
xcode8.2
roracle
image-registration
non-linear-regression
lvalue
case-when
kendo-editor
apache-modules
rcharts
swiperefreshlayout
scheduledexecutorservice
ipywidgets
multi-touch
dcount
flush
wif4.5
ioctl
in-app-billing
blank-line
screen-orientation
explain
erlog
gflags
mgwt
sablecc
android-audiomanager
maven-shade-plugin
famo.us
string.format
encryption-symmetric
smacss
igor
rpg
code-complexity
pre
property-injection
sensormanager
objective-c-runtime
blobstorage
jbox2d
oembed
significant-digits
lynx
wndproc
getproperty
workflow-manager-1.x
image-formats
msinfo32
kaazing
yuidoc
microblogging
dynamic-c
data-dump
sql-parametrized-query
kernel32
printqueue
active-record-query
hungarian-notation
mygeneration
mongrel
multiple-users
mysqli-multi-query
spring-modules
jquery-effects
icon-language
activex-exe
parentid

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App