TransientObjectException - 对象引用了一个未保存的瞬时实例 - 在刷新之前保存该瞬时实例。

22

我已经找到了几个不错的可能的答案来回答我的问题,但这是关于从Hibernate 3.4.0GA升级到Hibernate 4.1.8的问题。在以前的版本下它曾经能够工作,而我已经费尽了心思去寻找为什么它在这个新版本中出现错误。

我得到了一个

org.hibernate.TransientObjectException: 对象引用了一个未保存的临时实例 - 在刷新之前保存该临时实例:com.test.server.domain.model.NoteItem.note -> com.test.server.domain.model.Note

任何帮助都将会很棒。

以下是我的类。

@MappedSuperclass
public abstract class EntityBase implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    protected Long id;

    @Version
    @Column(name = "VERSION")
    protected Long version; 

    public Long getId() {
        return id;
    }

    public Long getVersion() {
        return version;
    }

    protected static final EntityManager entityManager() {
        return EntityManagerUtil.getEntityManager();
    }
}

@Entity
@Table(name = "WORK_BOOK")
public class WorkBook extends EntityBase {
    private static final long serialVersionUID = 1L;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "NOTE_ID")
    private Note note;

    public WorkBook() {
        super();
    }

    public Note getNote() {
        return note;
    }

    public void setNote(Note note) {
        this.note = note;
    }

    public WorkBook persist() {
        EntityManager em = entityManager();
        EntityTransaction tx = em.getTransaction();

        if (tx.isActive()) {
            return em.merge(this);
        } else {
            tx.begin();
            WorkBook saved = em.merge(this);
            tx.commit();
            return saved;
        }
    }
}

@Entity
@Table(name = "NOTE")
public class Note extends EntityBase {
    private static final long serialVersionUID = 1L;

    @OneToOne(mappedBy = "note")
    private WorkBook workBook;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "note")
    private List<NoteItem> notes = new ArrayList<NoteItem>();

    public WorkBook getWorkBook() {
        return workBook;
    }

    public List<NoteItem> getNotes() {
        return notes;
    }

    public void setWorkBook(WorkBook workBook) {
        this.workBook = workBook;
    }

    public void setNotes(List<NoteItem> notes) {
        if (notes != null) {
            for (NoteItem ni : notes) {
                ni.setNote(this);               
            }
        }

        this.notes = notes;
    }
}   

@Entity
@Table(name = "NOTE_ITEM")
public class NoteItem extends EntityBase {
    private static final long serialVersionUID = 1L;

    @Column(name = "NOTE_NAME")
    private String noteName;

    @Column(name = "NOTE_TEXT")
    private String noteText;

    @Column(name = "NOTE_DATE")
    private Date noteDate;

    @Column(name = "NOTE_CREATOR")
    private String noteCreator;

    @Column(name = "NOTE_CREATOR_ID")
    private Integer noteCreatorId;

    @ManyToOne
    @JoinColumn(name = "NOTE_ID", updatable = true)
    private Note note;

    public String getNoteName() {
        return noteName;
    }

    public void setNoteName(String noteName) {
        this.noteName = noteName;
    }

    public String getNoteText() {
        return noteText;
    }

    public void setNoteText(String noteText) {
        this.noteText = noteText;
    }

    public Date getNoteDate() {
        return noteDate;
    }

    public void setNoteDate(Date noteDate) {
        this.noteDate = noteDate;
    }

    public String getNoteCreator() {
        return noteCreator;
    }

    public void setNoteCreator(String noteCreator) {
        this.noteCreator = noteCreator;
    }

    public Integer getNoteCreatorId() {
        return noteCreatorId;
    }

    public void setNoteCreatorId(Integer noteCreatorId) {
        this.noteCreatorId = noteCreatorId;
    }

    public Note getNote() {
        return note;
    }

    public void setNote(Note note) {
        this.note = note;
    }

    public NoteItem create() {
        return new NoteItem();
    }
}   

2个回答

33

NoteItem 引用了一个尚未被保存的瞬态实例 Note,在此之前必须先保存它。因此,在属性 note 上指定 "Cascade.all",或者先调用 Note 的 saveOrUpdate 方法。


我没听懂,有教程可用或者你能再解释一下吗?Note是如何瞬态的? - AZ_
1
NoteItem noteItem = ; noteItem.Note = new Note(); session.Save(noteItem); - Firo

0

在引入乐观锁(@Version)后,我在所有PUT HTTP事务中都遇到了相同的错误。

在更新实体时,必须发送该实体的ID和版本。如果任何实体字段与其他实体相关,则对于该字段,我们还应提供ID和版本值,否则JPA将首先尝试持久化该相关实体。

示例:我们有两个实体--> Vehicle(id,Car,version) ; Car(id, version, brand) 要更新/持久化Vehicle实体,请确保车辆实体中的Car字段已提供id和version字段。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接