Java 如何使用Spring数据Jpa@manytomany从数据库中删除记录
我有客户端和通知,它们通过@ManyToMany关系链接,如下所示: 客户:Java 如何使用Spring数据Jpa@manytomany从数据库中删除记录,java,hibernate,spring-data-jpa,Java,Hibernate,Spring Data Jpa,我有客户端和通知,它们通过@ManyToMany关系链接,如下所示: 客户: @ManyToMany(fetch=FetchType.LAZY,级联={ cascade type.PERSIST, 级联类型合并 }) @JoinTable(name=“通知列表”, joinColumns=@JoinColumn(name=“client_ID”), inverseJoinColumns=@JoinColumn(name=“notification\u ID”) ) @Getter@Setter
@ManyToMany(fetch=FetchType.LAZY,级联={
cascade type.PERSIST,
级联类型合并
})
@JoinTable(name=“通知列表”,
joinColumns=@JoinColumn(name=“client_ID”),
inverseJoinColumns=@JoinColumn(name=“notification\u ID”)
)
@Getter@Setter
私有集通知=新建LinkedHashSet();
通知:
@ManyToMany(mappedBy = "notifications", fetch = FetchType.LAZY)
@Getter @Setter
private Set<Client> clients = new HashSet<>();
@ManyToMany(mappedBy=“notifications”,fetch=FetchType.LAZY)
@Getter@Setter
private Set clients=new HashSet();
我只希望删除链接,即auth_数据生成表中的条目。我该怎么做?我是否需要创建一个单独的JpaRepository,或者仅仅从客户端删除通知就足够了?已解决!
我向客户端实体类添加了方法:
public void removeNotification(Notification notification){
this.notifications.remove(notification);
notification.getClients().remove(this);
}
创建ClientServiceIml并执行以下操作:
@Override
public void removeNotifications(Client client) {
for(Notification notification : client.getNotifications()) {
client.removeNotification(notification);
}
clientRepository.save(client);
}
一切都很好。当你尝试时,你看到了什么?
@Override
public void removeNotifications(Client client) {
for(Notification notification : client.getNotifications()) {
client.removeNotification(notification);
}
clientRepository.save(client);
}