【问题标题】:TransientObjectException - object references an unsaved transient instance - save the transient instance before flushingTransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例
【发布时间】:2012-11-08 19:21:28
【问题描述】:

对于我的问题,我已经找到了一些很好的答案,但这是关于从 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();
    }
}   

【问题讨论】:

标签: hibernate jpa hibernate-onetomany


【解决方案1】:

在引入乐观锁定 (@Version) 后,我在所有 PUT HTTP 事务中都面临同样的错误

在更新实体时,必须发送该实体的 ID 和版本。 如果任何实体字段与其他实体相关,那么对于该字段,我们还应该提供 id 和版本值,而不是 JPA 尝试首先保留该相关实体

示例:我们有两个实体 --> Vehicle(id,Car,version) ; 汽车(id、版本、品牌) 更新/保留车辆实体确保车辆实体中的 Car 字段提供了 id 和 version 字段

【讨论】:

    【解决方案2】:

    NoteItem 引用了之前必须保存的Note 的瞬态(尚未保存)实例。因此,请在属性注释上指定“Cascade.all”或先在注释上调用 saveorupdate。

    【讨论】:

    • 我没听懂,有没有可用的教程或者你可以进一步解释。 Note 是如何瞬态的?
    • NoteItem noteItem = ; noteItem.Note = new Note(); session.Save(noteItem);
    猜你喜欢
    • 2012-03-21
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2021-03-03
    • 2013-11-30
    • 2017-04-12
    相关资源
    最近更新 更多