删除频道的正确方法是什么?我有一个一对一(用户到频道)的关系
Channel:
@OneToOne(mappedBy = "channel")
Private User user;
User:
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
private Channel channel;
当我删除User时,通道被安全删除,但是当我删除通道时,我得到错误
(deleted object would be re-saved by cascade (remove deleted object from associations): [ua.com.alevel.persistence.entity.channel.Channel#1])
我明白我需要从用户中删除通道,首先更新用户,然后删除通道,但它需要我几个操作
User user = userService.findByEmail(userEmail);
Channel channel = user.getChannel();
user.setChannel(null);
userService.update(user);
channelService.delete(channel);
可以把它做成这样吗?
channelService.delete(userService.findByEmail(userEmail).getChannel());
如果我没有正确地表达这个问题,我很抱歉,我只是在这方面不是很有经验,我试图尽可能详细地描述问题。
对不起,只是从用户中删除通道并更新用户。
User user = userService.findByEmail(userEmail);
user.setChannel(null);
userService.update(user);