简体   繁体   English

TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

[英]TransientObjectException - object references an unsaved transient instance - save the transient instance before flushing

I've come across a few good possible answers to my questions, but this is regarding an upgrade from Hibernate 3.4.0GA to Hibernate 4.1.8.对于我的问题,我遇到了一些很好的可能答案,但这是关于从 Hibernate 3.4.0GA 升级到 Hibernate 4.1.8。 So this used to work under the previous version and I've searched high and low for why its breaking in this new version.所以这曾经在以前的版本下工作,我已经在高低搜索了为什么它在这个新版本中被打破。

I get a我得到一个

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.server.domain.model.NoteItem.note -> com.test.server.domain.model.Note org.hibernate.TransientObjectException:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.test.server.domain.model.NoteItem.note -> com.test.server.domain.model.Note

Any help would be great.任何帮助都会很棒。

Here are my classes.这是我的课。

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

NoteItem references a transient (not yet saved) instance of Note which has to be saved before. NoteItem引用一个临时的(尚未保存的) Note实例,该实例之前必须保存。 So specify "Cascade.all" on property note or call saveorupdate on Note first.因此,请在属性注释上指定“Cascade.all”或首先在注释上调用 saveorupdate。

I was facing the same error for all PUT HTTP transactions, after introducing optimistic locking (@Version)在引入乐观锁定(@Version)后,我对所有 PUT HTTP 事务都面临同样的错误

At the time of updating an entity it is mandatory to send id and version of that entity.在更新实体时,必须发送该实体的 id 和版本。 If any of the entity fields are related to other entities then for that field also we should provide id and version values, without that the JPA try to persist that related entity first如果任何实体字段与其他实体相关,那么对于该字段,我们也应该提供 id 和 version 值,而不是 JPA 尝试首先保留该相关实体

Example : we have two entities --> Vehicle (id,Car,version) ;示例:我们有两个实体 --> Vehicle (id,Car,version) ; Car (id, version, brand) to update/persist Vehicle entity make sure the Car field in vehicle entity has id and version fields provided Car (id, version, brand) to update/persist Vehicle 实体确保 Vehicle 实体中的 Car 字段提供了 id 和 version 字段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保存惰性子项导致:TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例 - Saving lazy child is causing: TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing TransientObjectException:对象引用了一个未保存的临时实例-在执行合并时在刷新之前保存该临时实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing when I am doing merge TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例 - object references an unsaved transient instance - save the transient instance before flushing 对象引用一个未保存的瞬态实例,在刷新之前保存瞬态实例: - Object references an unsaved transient instance save the transient instance before flushing: hibernate.TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例-休眠升级后 - hibernate.TransientObjectException:object references an unsaved transient instance-save the transient instance before flushing-after hibernate upgrade 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM