2012-11-20 8 views
17

फ़्लश करने से पहले क्षणिक उदाहरण को सहेजें, मेरे प्रश्नों के कुछ अच्छे संभावित उत्तरों में आया है, लेकिन यह हाइबरनेट 3.4.0GA से हाइबरनेट 4.1.8 में अपग्रेड के संबंध में है। तो यह पिछले संस्करण के तहत काम करता था और मैंने इस नए संस्करण में क्यों तोड़ने के लिए उच्च और निम्न खोज की है।TransientObjectException - ऑब्जेक्ट संदर्भ एक सहेजे गए क्षणिक उदाहरण -

मैं एक

org.hibernate.TransientObjectException मिलती है: वस्तु एक न सहेजा गया क्षणिक उदाहरण का संदर्भ - निस्तब्धता से पहले क्षणिक उदाहरण बचाने: -> कॉम com.test.server.domain.model.NoteItem.note। 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(); 
    } 
} 
+1

[ऑब्जेक्ट संदर्भों को एक सहेजे गए क्षणिक उदाहरण के संदर्भ में संभावित डुप्लिकेट - फ़्लशिंग से पहले क्षणिक उदाहरण को सहेजें] (http://stackoverflow.com/questions/2302802/object-references-an-unsaved-transient-instance-save-the- क्षणिक-उदाहरण-हो) –

उत्तर

30

NoteItem एक क्षणिक (अभी तक नहीं बचाया) Note का उदाहरण है, जिसमें से पहले बचाया जाना है संदर्भित करता है। तो संपत्ति नोट पर "Cascade.all" निर्दिष्ट करें या पहले नोट पर saveorupdate को कॉल करें।

+0

मुझे यह नहीं मिला, क्या कोई ट्यूटोरियल उपलब्ध है या आप आगे समझा सकते हैं। नोट कितना क्षणिक है? –

+1

'नोटइटम नोट Item =; नोट Item.Note = नया नोट(); session.Save (noteItem); ' – Firo

संबंधित मुद्दे