how to check if record exists in DB table?
hey guys. can smb help me out? in my repo class i have this:
but when i call this method, i get an error:
so i dont understand anymore. i just want to check if there are any records, i googled, tried it, but its obviously isnt working. what are the alternatives/fixes for this case? thanks in advance
23 Replies
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Are you using spring data jpa?
Also, you're selecting count and expecting it to return a boolean. A number is not a boolean
But if you just have a method
Optional<Entity> findByTransactionId(String transactionId)
It will give you back an optional of your entity if it exists
no annotations necessaryrunning query with count is better
query runs on db and just returns count
You just need to change the return type.
i found that this works:
boolean existsByTransactionId(@Param("transactionId") String transactionId
thanks for your time fellasIf you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
how?
you might have changed the query.
select query returning boolen if count>0.
something like that.
huh?
i didnt get you.
Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.
without the query annotation then?
might be.
CrudRepository (Spring Data Core 3.3.2 API)
declaration: package: org.springframework.data.repository, interface: CrudRepository
CrudRepository (Spring Data Core 3.3.2 API)
declaration: package: org.springframework.data.repository, interface: CrudRepository
see this.
I will wait until you see what's wrong here
transactionId might be a primary key.
assumptions
ok. sorry
I will not assume.
i am just analysing.
yup
ah yeah
OK, I couldn't find in the docs if existsBy was a thing
here there right.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.