I am getting
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: IncidentStatus.incident -> Incident when I create two new entities and associate them with a OneToOne relationship inside the same transaction. My client code looks like this:
Code:
emf = Persistence.createEntityManagerFactory("pu1");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
String id = args[0];
Incident i = em.find(Incident.class, id);
i = new Incident(id);
IncidentStatus ist = new IncidentStatus(id);
i.setIncidentStatus(ist);
ist.setIncident(i);
em.persist(i);
em.getTransaction().commit();
If I don't call ist.setIncident(i); in the above code, it works.Entity class definitions are supplied below.
Hibernate version: Hibernate EntityManager 3.2.0.CR1, Hibernate 3.2 cr3
Mapping documents:Code:
@Entity public class Incident {
@Id String id;
@OneToOne(cascade=CascadeType.ALL)
IncidentStatus incidentStatus;
public Incident(){}
public Incident(String id) { this.id = id; }
public IncidentStatus getIncidentStatus() { return incidentStatus; }
public void setIncidentStatus(IncidentStatus incidentStatus) {
this.incidentStatus = incidentStatus;
}
}
@Entity public class IncidentStatus {
@Id String id;
@OneToOne(mappedBy="incidentStatus")
Incident incident;
public IncidentStatus() {}
public IncidentStatus(String id) { this.id = id; }
public Incident getIncident() { return incident; }
public void setIncident(Incident incident) { this.incident = incident; }
}
Full stack trace of any exception that occurs:
Exception in thread "main" javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:63)
at Client.main(Client.java:22)
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: IncidentStatus.incident -> Incident
at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java:273)
at org.hibernate.engine.Cascade.cascade(Cascade.java:257)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:53)
... 1 more
Name and version of the database you are using:
Derby