OrphanRemoval给出了TransientObjectException:" object引用未保存的瞬态实例"即使cascadeType全部

时间:2017-06-21 10:34:19

标签: java hibernate jpa spring-data-jpa

我已经阅读了关于此异常的许多类似问题的答案,但其中大多数似乎缺少CascadeType.PERSIST / ALL。在我的情况下,这个CascadeType存在。

我来回试了一下,如果OneToMany设置中没有orphanRemoval = true,一切正常。但是,如果从ApplicationState中删除,我们需要删除错误事件。

缩小示例:

public class ApplicationState implements Persistable<UUID>

    @OneToMany(
        cascade = CascadeType.ALL,
        mappedBy = "owningApplicationState",
        orphanRemoval=true)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private List<ErrorIncident> errorIncidents = new ArrayList<>();

    public void addErrorIncident(ErrorIncident errorIncident) {
        errorIncident.setOwningApplicationState(this);
        errorIncidents.add(errorIncident);
    }
}

public class ErrorIncident implements Persistable<UUID> {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(
        name = "APPLICATION_STATE_GUID", 
        referencedColumnName = "GUID", 
        foreignKey = @ForeignKey(name = "FK_ERROR_INCIDENT_APP_STATE"))
    private ApplicationState owningApplicationState;
}

例外:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance beforeQuery flushing: sb1.vo.creditcard.acscore.domain.application.ErrorIncident

执行此操作时会发生:

applicationState.addErrorIncident(new ErrorIncident());
applicationStateRepository.saveAndFlush(applicationState);

不涉及删除。添加orphanRemoval会导致异常发生,而只是正常创建错误事件。

0 个答案:

没有答案