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=”” table=”A” schema=”TESTSCHEMA”>
<id name=”aId” type=”java.lang.Long”>
<column name=”A_ID” precision=”29″ scale=”0″ />
some more mapping elements
<many-to-one name=”bId” class=”” fetch=”select”>
<column name=”B_ID” precision=”29″ scale=”0″ />


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.


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

  1. Sathish

    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.

    1. Paras

      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.

  2. Pingback: 2010 in review « Techspace

  3. Alexej Greschtschak

    thank you for the idea

    by the way. If youre using annotations i.e. with seam. You have to set these attributes in @JoinColumn.

    private User user;

    @JoinColumn(updatable=false, insertable=false)
    private User user;

    have a nice day

  4. gogul

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

    1. Waqas Ahmed

      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(
      at org.hibernate.type.EntityType.getIdentifier(
      at org.hibernate.type.ManyToOneType.isDirty(
      at org.hibernate.type.ManyToOneType.isDirty(
      at org.hibernate.type.TypeHelper.findDirty(
      at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(
      at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(
      at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(
      at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(
      at org.hibernate.impl.SessionImpl.flush(
      at org.hibernate.impl.SessionImpl.managedFlush(
      at org.hibernate.transaction.JDBCTransaction.commit(
      at com.hrms.controllerLayer.ServiceProvider.savePersonalInfoNormalUser(
      at com.hrms.beans.PersonalInfoBean.saveHrPersonnalBasicInfo(
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(
      at java.lang.reflect.Method.invoke(
      at org.apache.el.parser.AstValue.invoke(
      at org.apache.el.MethodExpressionImpl.invoke(
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(
      at com.sun.faces.application.ActionListenerImpl.processAction(
      at javax.faces.component.UICommand.broadcast(
      at javax.faces.component.UIData.broadcast(
      at javax.faces.component.UIViewRoot.broadcastEvents(
      at javax.faces.component.UIViewRoot.processApplication(
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(
      at com.sun.faces.lifecycle.Phase.doPhase(
      at com.sun.faces.lifecycle.LifecycleImpl.execute(
      at javax.faces.webapp.FacesServlet.service(
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(
      at org.apache.catalina.core.StandardWrapperValve.invoke(
      at org.apache.catalina.core.StandardContextValve.invoke(
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
      at org.apache.catalina.core.StandardHostValve.invoke(
      at org.apache.catalina.valves.ErrorReportValve.invoke(
      at org.apache.catalina.valves.AccessLogValve.invoke(
      at org.apache.catalina.core.StandardEngineValve.invoke(
      at org.apache.catalina.connector.CoyoteAdapter.service(
      at org.apache.coyote.http11.AbstractHttp11Processor.process(
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
      at java.util.concurrent.ThreadPoolExecutor$

    2. Paras

      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.


  5. Sravanthi

    public class Job implements Serializable {
    private String jobAlias;

    private String jobTitle;

    private String jobPurpose;

    private String jobDescription;

    private String jobString;

    private String targetPath;

    private String ftpFilename;

    private String ftpJobAlias;

    private String captureRun;

    private String captureRc;

    private String captureOutput;

    private String jobSynchType;

    private short expectedRunTime;

    private String changeReason;

    @Column(name = “USERID”)
    private String userid;

    @Column(name = “DTUPDATE”)
    private Timestamp dtupdate;

    private Server serverAlias;

    private Set webUiJobHistCollection;

    private Set webUiJobFunctionCollection;

    private Set webUiJobLockCollection;

    public class JobFunction implements Serializable {
    private JobFunctionPK pk;

    private short panelSequence;

    private String changeReason;

    private String userid;

    private Timestamp dtupdate;

    @JoinColumn(name=”FUNCTION_ID” ,insertable=false, updatable=false)
    private Function functionId;

    @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:”

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

    }“–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();

    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));
    fun = (JobFunction)criteria1.list();
    return fun;
    Can u pl help me out??

  6. Githehu Kinu


    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?

  7. Siddhant

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

  8. dsea

    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)


Leave a Reply

Your email address will not be published. Required fields are marked *