我收到“ java.lang.IllegalStateException:即将执行操作,但是有未解决的实体插入操作。”使用休眠模式更新实体时发生错误。
我使用继承将一个表映射到多个对象。实体RecurringEventEntity与InstanceEntity具有OneToMany关系,RecurringEventEntity事件和InstanceEntity都从AbstractEvent派生。我想做的是使用一些数据更新RecurringEventEntity-此更新可能需要创建与此RecurringEventEntity相关的新(或删除)InstanceEntity。我想定义Cascade,以便当我在RecurringEventEntity中更新Set并保存RecurringEventEntity时,将同时保存RecurringEventEntity和所有更改的InstanceEntities。知道为什么一开始会提到异常以及如何解决此问题吗?

这是简化的代码(模型和操作方法)

@Entity
@Table(name = "event", schema = "", catalog = "")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="`type`",
        discriminatorType=DiscriminatorType.STRING
)
abstract public class AbstractEvent {
    public enum EventType {
        normal,
        recurring,
        new_instance,
        original_instance,
        instance
    }
    private Long idEvent;
    private Date eventStart;
    private Date eventEnd;
    private EventType type;
    private String eventName;
    private Date createdDate;
    private Date modifiedDate;

    public AbstractEvent() {
        this.idEvent = null;
        this.createdDate = new Date();
    }

    @Id
    @Column(name = "id_event", nullable = false, insertable = true, updatable = true, length = 32, precision = 0)
    @GeneratedValue(generator = "sequence")
    @SequenceGenerator(name = "sequence", sequenceName = "cal_sequence")
    public Long getIdEvent() {
        return idEvent;
    }
    protected void setIdEvent(Long idEvent) { this.idEvent = idEvent; }

    @Basic
    @Column(name = "event_start", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    public Date getEventStart() {
        return eventStart;
    }
    public void setEventStart(Date eventStart) {
        this.eventStart = eventStart;
    }

    @Basic
    @Column(name = "event_end", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
    public Date getEventEnd() {
        return eventEnd;
    }
    public void setEventEnd(Date eventEnd) {
        this.eventEnd = eventEnd;
    }

    @Basic
    @Column(name = "type", nullable = false, insertable = false, updatable = false, length = 20, precision = 0)
    @Enumerated(EnumType.STRING)
    public EventType getType() {
        return type;
    }
    public void setType(EventType type) {
        this.type = type;
    }

    @Basic
    @Column(name = "event_name", nullable = false, insertable = true, updatable = true, length = 200, precision = 0)
    public String getEventName() {
        return eventName;
    }
    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    @Basic
    @Column(name = "created", nullable = false, insertable = true, updatable = false)
    public Date getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(Date created) {
        this.createdDate = created;
    }

    @Basic @Version
    @Column(name = "modified", nullable = true, insertable = true, updatable = true)
    public Date getModifiedDate() {
        return modifiedDate;
    }
    public void setModifiedDate(Date modified) {
        this.modifiedDate = modified;
    }
}

@Entity
@DiscriminatorValue("recurring")
public class RecurringEventEntity extends AbstractEvent {

    private Set<AbstractEventInstance> instances;

    @OneToMany(mappedBy = "recurringEventEntity",fetch = FetchType.LAZY)
    @Cascade(value = {org.hibernate.annotations.CascadeType.ALL})
    public Set<EventInstance> getInstances() {
        return this.instances;
    }
    public void setInstances(Set<EventInstance> instances) {
        this.instances = instances;
    }
}

@Entity
@DiscriminatorValue("instance")
public class InstanceEntity extends AbstractEventInstance {
    private RecurringEventEntity recurringEventEntity;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "`recurring_event`", referencedColumnName = "`id_event`", nullable = false, insertable = false, updatable = false)
    public RecurringEventEntity getRecurringEventEntity() {
        return recurringEventEntity;
    }

    public void setRecurringEventEntity(RecurringEventEntity recurringEventEntity) {
        this.recurringEventEntity = recurringEventEntity;
    }
}

@Transactional
public void operation(Long event_id, Object data)
{
    Session session = this.getSession();

    RecurringEvent rEvent = session.get(AbstractEvent.class, event_id);
    updateEvent(rEvent, data);
    session.saveOrUpdate(rEvent);
}

public void updateEvent(RecurringEvent rEvent, Object data)
{
    ...
    InstanceEntity instance = new InstanceEntity();
    ...
    rEvent.getInstances().add(instance);
}

最佳答案

好的,我发现了问题。我的问题中没有提到它,但是实例具有循环引用(打算使用)。删除循环引用后,它可以工作。

10-08 17:31