Spring JPA repository: prevent update on save

后端 未结 3 866
广开言路
广开言路 2020-12-16 05:28

My user DB table looks like this:

CREATE TABLE user (
    username VARCHAR(32) PRIMARY KEY,
    first_name VARCHAR(256) NOT NULL,
    last_name          


        
3条回答
  •  眼角桃花
    2020-12-16 05:39

    Instead of this.userRepository.save(user) , can you try this.userRepository.saveAndFlush(user)

    My best guess is, it will make your entity detached and as per the JPA documentation, it states an EntityExistsException is thrown by the persist method when the object passed in is a detached entity. Or any other PersistenceException when the persistence context is flushed or the transaction is committed.

提交回复
热议问题