可选的@OneToOne和更新不起作用

问题描述 投票:0回答:1

我有一个LiveStreamInformation对象可以属于TwitchAccountYoutubeAccountMixer 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(不存在),为什么它不能正确更新?

hibernate spring-boot jpa
1个回答
1
投票

您需要在twitchAccount实例上设置LiveStreamInformation

persisted = liveStreamInformationService.create(info);
persisted.setTwitchAccount(twitchAccount);// Here is the new line
twitchAccount.setLiveStreamInformation(persisted);
update(twitchAccount);
© www.soinside.com 2019 - 2024. All rights reserved.