本文介绍了TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了几个很好的答案,但是这是关于从Hibernate 3.4.0GA升级到Hibernate 4.1.8的。因此,这用于在以前的版本下工作,我已经搜索了高和低为什么它打破这个新版本。
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. So this used to work under the previous version and I've searched high and low for why its breaking in this new version.
我得到一个
任何帮助都很棒。
以下是我的类。
@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
引用一个临时(尚未保存的)注意
的实例,它必须在之前保存。因此,请在属性说明中指定Cascade.all,或者先在Note上调用saveorupdate。
NoteItem
references a transient (not yet saved) instance of Note
which has to be saved before. So specify "Cascade.all" on property note or call saveorupdate on Note first.
这篇关于TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!