Home > other >  I tried to update the database by jpa but it wont update or gets duplicate
I tried to update the database by jpa but it wont update or gets duplicate

Time:08-22

I tried to update the table by jpa.

CREATE TABLE `MagicnotifyCart` (
    `uuid` VARCHAR(50),
    `userid` BIGINT(20),
    `desired_price` DECIMAL(10,2),
    FOREIGN KEY (`uuid`) REFERENCES `MagicnotifyUuidName` (`key`),
    FOREIGN KEY (`userid`) REFERENCES `User` (`id`)
);

the table i wanted to update is this one, and i tried to update it by jpa.

@Transactional
public void updateDesiredPrice(BigDecimal desired_price, String uuid, int userid){
    MagicnotifyCart magicnotifyCart = cartRepository.findMagicnotifyCartById_UuidAndId_Userid(uuid, userid);
    
    magicnotifyCart.getId().setDesired_price(desired_price);
    cartRepository.save(magicnotifyCart);
}

by using this, i expect to update desired_price null to desired_price, but it won't update when i try this code.

I debugged the code and I see magicnotifyCart.getId().getDesired_price() becomes the price as I expected, but when it comes to save the value it won't update. What should I do if I want to update the value of desired_price by using jpa?

CodePudding user response:

There is a possibility that magicnotifyCart.getId() returns a different/new detached MagicnotifyCart instance, the other magicnotifyCart is already non-transient instance of magicnotifyCart so ideally you would just need :

magicnotifyCart.setDesired_price(desired_price);
  • Related