org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:This is the problemTable A was referring to the talbe B using a foreign key
hbm.xml for A looked like this
<class name=”com.xxx.A” table=”A” schema=”TESTSCHEMA”>
<id name=”aId” type=”java.lang.Long”>
<column name=”A_ID” precision=”29″ scale=”0″ />
</id>…………………..
some more mapping elements
…………………..
…………………..
<many-to-one name=”bId” class=”com.xxx.B” fetch=”select”>
<column name=”B_ID” precision=”29″ scale=”0″ />
</many-to-one>
…………………..
I just want to save A with some new value and A will refer to B using some foreign key. But in my transaction I am not going to change B and save the changes. Therefore the many-to-one mapping above is wrong. It does not tell hibernate that class b is immutant. That is it is not going to change. Hibernate thinks that the referred table is not saved therefore it complains and says
org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:
Please note that in this case B is transient object. If you don’t really want to save the value of B(transient object) in A , then to get rid of this problem change the many-to-one mapping as
update=”false” insert=”false” fetch=”select”>
But if you want to save the value of B then see my other post
That is, tell hibernate that you are not going to change the values of B, you are just fetching it.
Hi man you made my day ….
Thanks
Excellent post. Keep up the good work.
Hi,
Thank you very much, you solved my problem 😉
Have a nice day,
Khalid
Thanks for sharing, one quick clarification. I was not able to insert the data for mapped columns when flags are set to false. But when I set flags to true, everything works well.
Hi Sathish,
Yes when you set the flag to true Hibernate will do the inserts for those mapped columns. setting fields to false tells the hibernate that this fields are read only and hibernate should not update them.
Thanks.. 🙂
There is obviously a lot to know about this. There are some good points here.
I’m Out! 🙂
Hey, you save me a long long day tonight..
Really thanks to you.
Thank u very much boss…
I wastred 1 hr for this idiotic bug… thaks a lot
Pingback: 2010 in review « Techspace
thank you for the idea
by the way. If youre using annotations i.e. with seam. You have to set these attributes in @JoinColumn.
so
@ManyToOne
private User user;
becomes
@ManyToOne
@JoinColumn(updatable=false, insertable=false)
private User user;
have a nice day
I am having the same scenario…
But for one to many relationship.
Bag tag is not supporting update=”false” insert=”false”
Any other way?
Thank you, this remark seve me a lot of time
Thanks a lot 🙂
Thanks a lot. your the man..:-)
Really it helps me lot..
i have same issue but didnot solve kindly help me
this is exception
org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing: com.hrms.persistanceLayer.HrAddress
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.hrms.modelLayer.BaseDAO.save(BaseDAO.java:101)
at com.hrms.controllerLayer.ServiceProvider.savePersonalInfoNormalUser(ServiceProvider.java:99)
at com.hrms.beans.PersonalInfoBean.saveHrPersonnalBasicInfo(PersonalInfoBean.java:1176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIData.broadcast(UIData.java:912)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Hi Waqas,
I am afraid that without looking at your configuration files it would be difficult to provide any comments. Your best friend now will be google. I am not as deeply involved in Hibernate right now as I was at the time I wrote the post. So I might not have up-to-date info on that.
Thanks for reading my post and taking time to comment.
Regards,
Paras
@Entity
@Table(name=”WEB_UI_JOB”)
public class Job implements Serializable {
@Id
@Column(name=”JOB_ALIAS”)
private String jobAlias;
@Column(name=”JOB_TITLE”)
private String jobTitle;
@Column(name=”JOB_PURPOSE”)
private String jobPurpose;
@Column(name=”JOB_DESCRIPTION”)
private String jobDescription;
@Column(name=”JOB_STRING”)
private String jobString;
@Column(name=”TARGET_PATH”)
private String targetPath;
@Column(name=”FTP_FILENAME”)
private String ftpFilename;
@Column(name=”FTP_JOB_ALIAS”)
private String ftpJobAlias;
@Column(name=”CAPTURE_RUN”)
private String captureRun;
@Column(name=”CAPTURE_RC”)
private String captureRc;
@Column(name=”CAPTURE_OUTPUT”)
private String captureOutput;
@Column(name=”JOB_SYNCH_TYPE”)
private String jobSynchType;
@Column(name=”EXPECTED_RUN_TIME”)
private short expectedRunTime;
@Column(name=”CHANGE_REASON”)
private String changeReason;
@Column(name = “USERID”)
private String userid;
@Column(name = “DTUPDATE”)
private Timestamp dtupdate;
@ManyToOne
@JoinColumn(name=”SERVER_ALIAS”)
private Server serverAlias;
@OneToMany(mappedBy=”jobAlias”)
private Set webUiJobHistCollection;
@OneToMany(mappedBy=”jobAlias”)
private Set webUiJobFunctionCollection;
@OneToMany(mappedBy=”jobAlias”)
private Set webUiJobLockCollection;
@Entity
@Table(name=”WEB_UI_JOB_FUNCTION”)
public class JobFunction implements Serializable {
@EmbeddedId
private JobFunctionPK pk;
@Column(name=”PANEL_SEQUENCE”)
private short panelSequence;
@Column(name=”CHANGE_REASON”)
private String changeReason;
private String userid;
private Timestamp dtupdate;
@ManyToOne
@JoinColumn(name=”FUNCTION_ID” ,insertable=false, updatable=false)
private Function functionId;
@ManyToOne
@JoinColumn(name=”JOB_ALIAS”,insertable=false, updatable=false )
private Job jobAlias;
Hi, above are my model classes
when running the below query..its thorwing the following exception
“org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing: com.ibm.shuttle.model.Job”
query:
public JobFunction search(String jobAlias, String serverAlias, String jobTitle,String jobType){
JobFunction fun=null;
//jobfun=(JobFunction)baseService.findById(JobFunction.class, jobType);
Server server = null;
if (serverAlias != null) {
server = serverService.findById(serverAlias);
}
LOG.info(“–jobAlias->” + jobAlias + “–serverAlias–>” + serverAlias
+ “–jobTitle–>” + jobTitle );
Session session = PersistenceSessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Job.class);
if (jobAlias != null) {
criteria.add(Restrictions.eq(“jobAlias”, jobAlias));
}
if (jobTitle != null) {
criteria.add(Restrictions.eq(“jobTitle”, jobTitle));
}
if (serverAlias != null) {
criteria.add(Restrictions.eq(“serverAlias”, server));
}
List jobList = criteria.list();
Job job = new Job();
job.setJobList(jobList);
//System.out.println(“jobList–>”+jobList.getJobAlias());
if(jobList != null){
System.out.println(“in joblist!=null”);
Criteria criteria1 = session.createCriteria(JobFunction.class);
if (jobType != null) {
criteria1.add(Restrictions.eq(“pk.functionId2”, jobType));
}
criteria1.add(Restrictions.eq(“jobAlias”,job));
fun = (JobFunction)criteria1.list();
fun.getPk().getJobAlias2();
}
return fun;
}
Can u pl help me out??
Hello,
So the purpose is to ensure the field B_ID is not inserted or updated from inserts or updated to class A right? What I do not understand is, if you do not want to insert the value or even update it, from where are you going to insert value for this field?
Today i got this Error I have two class A and B.A has primary key ref to B.I changed in B.hbm.xml as
previously I make it as .After making these changes it will resolve the issue…..
like Githehu Kinu: in the transaction, if i want to change A.B_ID but not object B its self => how could I do?
(list of B is displayed on tag on the A’s form for example)
thanks
I am still getting the same error . Here is the details
http://stackoverflow.com/questions/18895585/hibernate-version-annotation-and-object-references-an-unsaved-transient-instanc