简体   繁体   中英

Update by JPA native query

I am using spring boot repository with native query. While I am trying to update few fields by query and supply wrong values it is not throwing any exception. here is my code,

@Modifying(clearAutomatically = true)
@Query(value = "update tbl_user set is_phone_Verified=true, mobile_verification_otp='XXX', updated_At=:updatedAt where " +
        "phone_number=:registeredMobile and " +
        "mobile_verification_otp=:phoneVerificationOtp", nativeQuery = true)
void updateMobileVerified(@Param("registeredMobile") String registeredMobile,
                         @Param("phoneVerificationOtp") String phoneVerificationOtp,
                         @Param("updatedAt") Date updatedAt);

Now, the issue is if I supply even wrong otp value, it is not throwing any exception. From service I am calling this method. and if there is no exception then I am returning true. Please find the service method.

@Override
public Boolean mobileVerification(String registeredMobile, String phoneVerificationOtp) {
    try{
        Date date = new Date();
        userRepository.updateMobileVerified(registeredMobile,phoneVerificationOtp, date);
        return true;
    } catch(Exception e) {
        return false;
    }
}

Can someone please suggest me some way how can I determine if the update is successful.

Thanks.

If you would like to determine whether any rows were updated, the updateMobileVerified method can be defined to return int instead of void to indicate the number of rows updated

It seems that you are updating an unknown entity in the database. I highly recommend looking for a tbl_user entity by a unique id, like user_id or something like that. If the user doesn't exist, you either throw an exception or return false. In the case the given tbl_user was found, you basically update your desired attributes. This way, you force a strong control over updating users' data. I hope this might clarify your vision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM