Delete entity from many to many in jpa

Viewed 668

I have an Entity

@Entity
@Table(name="user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "friends")
    private Set<User> friends;
}

Currently I remove a friend by getting first all the friends from database which is not efficient/optimal

@Override
@Transactional
public void deleteFriend(String username, String friend) {
    User user = getUser(username);
    User other = getUser(friend);
    user.getFriends().remove(other);
    other.getFriends().remove(user);
}

Since it's not efficient I would like to use this query

public interface UserRepository extends PagingAndSortingRepository<User, Long>{
    void removeByUsernameAndFriendsUsername(String username, String otherUsername);
}

And when I try to delete a friend with this query I get an exception

    Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK8KCUM44FVPUPYW6F5BACCX25C: PUBLIC.COMMENT FOREIGN KEY(USER_ID) REFERENCES PUBLIC.USER(ID) (3)"; SQL statement:
delete from user where id=? and version=? [23503-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:425)
    at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:442)
    at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:317)
    at org.h2.table.Table.fireConstraints(Table.java:980)
    at org.h2.table.Table.fireAfterRow(Table.java:998)
    at org.h2.command.dml.Delete.update(Delete.java:101)
    at org.h2.command.CommandContainer.update(CommandContainer.java:101)
    at org.h2.command.Command.executeUpdate(Command.java:260)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:164)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:150)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 68 more

Do you know if it's possible to delete it with @NamedQuery or @Query where I don't have to get all the friends first? Something like the query below.

@Query(value = "delete from User U join U.friends friends where U.username = ?1 and friends.username = ?2")

Here's the comment entity

@Entity
@Table(name="comment")
public class Comment {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @NotNull
    @Size(min=1)
    @Column(name="text", nullable=false)
    private String text;

    @NotNull
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;
}
1 Answers
Related