sách gpt4 ai đã đi

hibernate - TransientObjectException - đối tượng tham chiếu đến một thể hiện transient chưa được lưu - lưu thể hiện transient trước khi xóa

In lại 作者:行者123 更新时间:2023-12-02 20:30:12 28 4
mua khóa gpt4 Nike

我遇到了一些可能的问题答案,但这是关于从 Hibernate 3.4.0GA 升级到 Hibernate 4.1.8 的问题。所以这曾经在以前的版本下工作,我已经四处搜索了为什么它在这个新版本中出现了问题。

我得到一个

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

Bất kỳ sự giúp đỡ nào cũng đều tuyệt vời.

这是我的类(class)。

@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() {
siêu();
}

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);
} khác {
tx. bắt đầu();
WorkBook saved = em.merge(this);
tx. cam kết();
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 notes = new ArrayList();

public WorkBook getWorkBook() {
return workBook;
}

public List getNotes() {
return notes;
}

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

public void setNotes(List 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();
}
}

1 Câu trả lời

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

关于hibernate - TransientObjectException - 对象引用未保存的 transient 实例 - 在刷新之前保存 transient 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13478055/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com