![](/img/trans.png)
[英]Saving lazy child is causing: TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
[英]TransientObjectException - object references an unsaved transient instance - save the transient instance before flushing
對於我的問題,我遇到了一些很好的可能答案,但這是關於從 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();
}
}
NoteItem
引用一個臨時的(尚未保存的) Note
實例,該實例之前必須保存。 因此,請在屬性注釋上指定“Cascade.all”或首先在注釋上調用 saveorupdate。
在引入樂觀鎖定(@Version)后,我對所有 PUT HTTP 事務都面臨同樣的錯誤
在更新實體時,必須發送該實體的 id 和版本。 如果任何實體字段與其他實體相關,那么對於該字段,我們也應該提供 id 和 version 值,而不是 JPA 嘗試首先保留該相關實體
示例:我們有兩個實體 --> Vehicle (id,Car,version) ; Car (id, version, brand) to update/persist Vehicle 實體確保 Vehicle 實體中的 Car 字段提供了 id 和 version 字段
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.