我有一个LiveStreamInformation
对象可以属于TwitchAccount
,YoutubeAccount
或Mixer Account
的情况。实际上,LiveStreamInformation
对象将仅属于这些Account对象之一。这些是我设置的映射,但是当我执行以下操作时:]
TwitchAccount account = findById(id);
account.setLiveStreamInformationObject(info);
update(account);
未进行更改(但无提示失败)。以下是有关如何设置休眠映射的摘要:
TwitchAccount:
public class TwitchAccount {
@OneToOne(mappedBy = "twitchAccount", cascade = CascadeType.ALL)
private LiveStreamInformation liveStreamInformation;
}
LiveStreamInformation:
public class LiveStreamInformation {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "liveStreamInformationId")
private TwitchAccount twitchAccount;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "liveStreamInformationId", referencedColumnName = "liveStreamInformationId")
private MixerAccount mixerAccount;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "liveStreamInformationId", referencedColumnName = "liveStreamInformationId")
private YoutubeAccount youtubeAccount;
}
请注意,YoutubeAccount和MixerAccount具有与TwitchAccount相同的确切结构/映射(只是带有各自的mappedBy)
这是我们如何尝试将实时流信息对象设置为帐户的示例
if (info != null) {
LiveStreamInformationResponseDto dto = info.convertToResponseDto();
dto.setUser(f.getFollows().convertToResponseDto());
liveStreams.add(dto);
// add/update to db for future checks
persisted = liveStreamInformationService.create(info);
twitchAccount.setLiveStreamInformation(persisted);
update(twitchAccount);
}
更新方法如下所示(并且在我的应用程序中的所有其他情况下都可以使用)
@Override
public void update(final T entity) {
Preconditions.checkNotNull(entity);
getDao().save(entity);
}
我想我的问题是我是否有映射设置,因为它实际上是@OneToOne或@OneToNone(不存在),为什么它不能正确更新?
您需要在twitchAccount
实例上设置LiveStreamInformation
:
persisted = liveStreamInformationService.create(info);
persisted.setTwitchAccount(twitchAccount);// Here is the new line
twitchAccount.setLiveStreamInformation(persisted);
update(twitchAccount);