2012-11-20 38 views
17

Tôi đã xem qua một vài câu trả lời hay cho câu hỏi của tôi, nhưng điều này liên quan đến việc nâng cấp từ Hibernate 3.4.0GA lên Hibernate 4.1.8. Vì vậy, điều này được sử dụng để làm việc theo phiên bản trước và tôi đã tìm kiếm cao và thấp cho lý do tại sao nó phá vỡ trong phiên bản mới này.TransientObjectException - đối tượng tham chiếu một trường hợp tạm thời chưa lưu - lưu thể hiện thoáng qua trước khi xóa

tôi nhận được một org.hibernate.TransientObjectException

: đối tượng tham chiếu một thể hiện thoáng qua chưa được lưu - lưu dụ thoáng qua trước khi xả nước: com.test.server.domain.model.NoteItem.note -> com. test.server.domain.model.Note

Mọi trợ giúp sẽ tuyệt vời.

Đây là các lớp học của tôi.

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

có thể trùng lặp của [đối tượng tham chiếu một trường hợp tạm thời chưa được lưu - lưu phiên bản tạm thời trước khi xả] (http://stackoverflow.com/questions/2302802/object-references-an-unsaved-transient-instance-save-the- transient-instance-be) –

Trả lời

30

NoteItem tham chiếu một thoáng (chưa lưu) thể hiện của Note mà phải được lưu trước đó. Vì vậy, chỉ định "Cascade.all" trên ghi chú thuộc tính hoặc gọi saveorupdate trên Note trước.

+0

tôi đã không nhận được điều đó, là có bất kỳ hướng dẫn có sẵn hoặc bạn có thể giải thích thêm. Làm thế nào lưu ý là thoáng qua? –

+1

'NoteItem noteItem =; noteItem.Note = new Note(); session.Save (noteItem); ' – Firo

Các vấn đề liên quan