我有一个user_id和favorite_property_id的联结表现在我想要删除用户的收藏属性,我在存储库中尝试了以下方法,但它无法正常工作,有没有人知道如何删除他的条目来自连接表?
User.java
@Entity
public class User implements Serializable, UserGetters {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Property> favouriteProperty;
public Set<Property> getFavouriteProperty() {
return favouriteProperty;
}
public void setFavouriteProperty(Set<Property> favouriteProperty) {
this.favouriteProperty = favouriteProperty;
}
}
UserRepository
public interface UserRepository extends JpaRepository<User, Long> {
//@Transactional
// Long deleteByFavouritePropertyId(@Param("favoriteProperty") Long favoriteProperty);
// @Query("delete favouriteProperty from user u where u.favouriteProperty.id=:favouriteProperty")
// void unmarkFavouriteProperty(@Param("favouriteProperty") Long favouriteProperty);
}
答案 0 :(得分:2)
你可以这样做:
User user = userRepository.findOne(userId);
user.getFavouriteProperty().remove(id);
userRepository.save(user);
答案 1 :(得分:1)
尝试使用:
Query query = session.createQuery("delete Category where id = :favorite_property_id ");
query.setParameter("favorite_property_id ", new Long(favoriteProperty));
int result = query.executeUpdate();
存储库文件中的。它会正常工作。
答案 2 :(得分:0)