如何解决java.lang.IllegalStateException:关于执行操作,但是有未解析的实体插入操作

时间:2014-05-16 18:23:12

标签: java hibernate illegalstateexception

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

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

@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);
}

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。在我的问题中没有提到它,但实例有循环引用(意图)。删除循环引用后,它可以正常工作。