Delete Not Working with JpaRepository

前端 未结 12 2030
情书的邮戳
情书的邮戳 2020-12-07 11:55

I have a spring 4 app where I\'m trying to delete an instance of an entity from my database. I have the following entity:

@Entity
public class Token impleme         


        
12条回答
  •  失恋的感觉
    2020-12-07 12:50

    One way is to use cascade = CascadeType.ALL like this in your userAccount service:

    @OneToMany(cascade = CascadeType.ALL)
    private List tokens;
    

    Then do something like the following (or similar logic)

    @Transactional
    public void deleteUserToken(Token token){
        userAccount.getTokens().remove(token);
    }
    

    Notice the @Transactional annotation. This will allow Spring (Hibernate) to know if you want to either persist, merge, or whatever it is you are doing in the method. AFAIK the example above should work as if you had no CascadeType set, and call JPARepository.delete(token).

提交回复
热议问题