Techspace

IT happens only in IT

org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:

I was getting this error today.
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.

 

January 14, 2008 - Posted by | Hibernate | ,

18 Comments »

  1. Hi man you made my day ….
    Thanks

    Comment by David | September 30, 2008 | Reply

  2. Excellent post. Keep up the good work.

    Comment by small | October 8, 2008 | Reply

  3. Hi,

    Thank you very much, you solved my problem ;-)

    Have a nice day,
    Khalid

    Comment by Khalid | February 3, 2009 | Reply

  4. 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.

    Comment by Sathish | August 11, 2009 | Reply

    • 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.

      Comment by Paras | August 12, 2009 | Reply

  5. Thanks.. :)

    Comment by Yogi | September 16, 2009 | Reply

  6. There is obviously a lot to know about this. There are some good points here.

    I’m Out! :)

    Comment by online stock trading guru | January 11, 2010 | Reply

  7. Hey, you save me a long long day tonight..
    Really thanks to you.

    Comment by Tomiito | January 13, 2010 | Reply

  8. Thank u very much boss…
    I wastred 1 hr for this idiotic bug… thaks a lot

    Comment by Kiran vadde | January 21, 2010 | Reply

  9. [...] The busiest day of the year was June 29th with 265 views. The most popular post that day was org.hibernate.TransientObjectException: object references an unsaved transient instance – save…. [...]

    Pingback by 2010 in review « Techspace | January 4, 2011 | Reply

  10. 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

    Comment by Alexej Greschtschak | January 6, 2011 | Reply

  11. I am having the same scenario…
    But for one to many relationship.
    Bag tag is not supporting update=”false” insert=”false”
    Any other way?

    Comment by gogul | February 10, 2011 | Reply

  12. Thank you, this remark seve me a lot of time

    Comment by zeki | March 1, 2011 | Reply

  13. Thanks a lot :)

    Comment by Yellen Seram | September 6, 2011 | Reply

  14. Thanks a lot. your the man..:-)

    Really it helps me lot..

    Comment by Vijay | September 30, 2011 | Reply

  15. i have same issue but didnot solve kindly help me

    Comment by Waqas Ahmed | November 11, 2011 | Reply

    • 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)

      Comment by Waqas Ahmed | November 11, 2011 | Reply

    • 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

      Comment by Paras | November 13, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.