why use Optional<MyClass> if you cant call any methods that MyClass has?
hey guys. can smb explain to me what wrong? im trying to retrieve an object from DB:
Optional<Payment> earlierPayment=paymentRepository.findByTransactionIdAndAmount(paymentConfirmationRequest.getTransactionId(), paymentConfirmationRequest.getAmount());
.
and for some reason i cant call any getters that my Payment
entity has. does it mean that id need to change the return type of findByTransactionIdAndAmount
from Optional<Payment>
to just Payment
? and then id loose the advantage Optional
gives me. that doesnt make any sense 😦
any help? thanks8 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.
well, look at it this way
you ask the database to look up a thing by an id or something
obviously that thing might not exist, so at the repository level an Optional makes sense
Then in your service layer, you might actually need the thing to do something with
At that point, what do you do if you don't have the thing?
Can the method even continue?
If it can't,
.orElseThrow
, if it can .(flat)map
wait a sec. it turns out that i can call
.get()
method on my Optional
and then id have the object, and then i could call the the metods my object hasUnknown User•5mo ago
Message Not Public
Sign In & Join Server To View
yep. but i see that im doing
isEmpty
on my optional earlier in my service
but thanks for telling me that it can be a pitfallIf 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.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
💤
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.