how to check if record exists in DB table?

hey guys. can smb help me out? in my repo class i have this:
@Query(value = "select count(1) from my_tablewhere transaction_id= :transactionId",nativeQuery = true)
boolean existsByTransactionId(@Param("transactionId") String transactionId);
@Query(value = "select count(1) from my_tablewhere transaction_id= :transactionId",nativeQuery = true)
boolean existsByTransactionId(@Param("transactionId") String transactionId);
but when i call this method, i get an error:
2024-07-24 11:14:58.459 ERROR 26832 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: class java.math.BigInteger cannot be cast to class java.lang.Boolean (java.math.BigInteger and java.lang.Boolean are in module java.base of loader 'bootstrap')] with root cause

java.lang.ClassCastException: class java.math.BigInteger cannot be cast to class java.lang.Boolean
2024-07-24 11:14:58.459 ERROR 26832 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: class java.math.BigInteger cannot be cast to class java.lang.Boolean (java.math.BigInteger and java.lang.Boolean are in module java.base of loader 'bootstrap')] with root cause

java.lang.ClassCastException: class java.math.BigInteger cannot be cast to class java.lang.Boolean
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
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
tjoener
tjoener5mo ago
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 necessary
webbdays
webbdays5mo ago
running query with count is better query runs on db and just returns count You just need to change the return type.
bambyzas
bambyzasOP5mo ago
i found that this works: boolean existsByTransactionId(@Param("transactionId") String transactionId thanks for your time fellas
JavaBot
JavaBot5mo ago
If 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.
webbdays
webbdays5mo ago
how? you might have changed the query. select query returning boolen if count>0. something like that.
bambyzas
bambyzasOP5mo ago
huh?
webbdays
webbdays5mo ago
i didnt get you.
JavaBot
JavaBot5mo ago
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!.
tjoener
tjoener5mo ago
without the query annotation then?
webbdays
webbdays5mo ago
might be.
webbdays
webbdays5mo ago
see this.
tjoener
tjoener5mo ago
I will wait until you see what's wrong here
webbdays
webbdays5mo ago
transactionId might be a primary key.
tjoener
tjoener5mo ago
assumptions
webbdays
webbdays5mo ago
ok. sorry I will not assume. i am just analysing.
bambyzas
bambyzasOP5mo ago
yup
tjoener
tjoener5mo ago
ah yeah OK, I couldn't find in the docs if existsBy was a thing
webbdays
webbdays5mo ago
here there right.
JavaBot
JavaBot5mo ago
💤 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.
Want results from more Discord servers?
Add your server