title: Hibernate Exception - Transient Object Exception
body: <p>I have been going through the web about this exception. I found many articles and tried to fix it but unfortunatley after all the time I spent I am still getting the same error.</p>
  
  <p>hbm.xml file..</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="AbstractLoadKey" table="loadkey" abstract="true"&gt;
          &lt;id name="id" type="long"&gt;
              &lt;generator class="org.hibernate.id.enhanced.SequenceStyleGenerator"&gt;
                  &lt;param name="sequence_name"&gt;hibernate_sequence&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
          &lt;discriminator type="string" length="1" column="subclass" not-null="true"/&gt;
          &lt;version name="version" type="long" column="version"/&gt;
          &lt;property name="insertTime" type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime" column="INSERTTIME"/&gt;
          &lt;property name="updateTime" type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime" column="UPDATETIME"/&gt;
          &lt;component name="origin" class="SortPlan"&gt;
              &lt;property name="country" type="string" length="2" column="orig_country" not-null="true" /&gt;
              &lt;property name="slic" type="string" length="5" column="orig_slic" not-null="true" /&gt;
              &lt;property name="portCode" type="string" length="5" column="orig_portcode" not-null="true" /&gt;
              &lt;many-to-one
                  name="code"
                  class="SortCode"
                  column="orig_sort_id"
                  foreign-key="domloadplan_origsort_fk"
                  index="domloadplan_origsort_fk"
                  not-null="true"
              /&gt;
  </code></pre>
  
  <p>I want to save the many-to-one property in the main table but not in the <code>SortCode</code> (Many-to-one related table)</p>
  
  <p>calling this method in controller:</p>
  
  <pre><code>dkey = domLoadKeyDao.read(dkey);
  
      @Transactional
      public DomLoadKey read(DomLoadKey domLoadKey) {
          Session s = getSessionFactory().getCurrentSession();
          Criteria c = s.createCriteria(getEntityClass());
          c.add(Restrictions.eq("origin.country", domLoadKey.getOrigin().getCountry()));
          c.add(Restrictions.eq("origin.slic", domLoadKey.getOrigin().getSlic()));
          .
          .
          .
          DomLoadKey result = (DomLoadKey) c.uniqueResult();
          if(result == null)
          return domLoadKey;
          return result;
  }
  </code></pre>
  
  <p>When I go in to see where it is failing, at this particular snippet it fails..instead of going to <code>success = true</code> it comes out of it..</p>
  
  <p>this snippet is from </p>
  
  <pre><code>sessionImpl.class
  
  boolean success = false;
          try {
              for( int i=0; i&lt;size; i++ ) {
                  final List currentResults = loaders[i].list(this);
                  currentResults.addAll(results);
                  results = currentResults;
              }
              success = true;
          }
          finally {
              dontFlushFromFind--;
              afterOperation(success);
          }
  </code></pre>
  
  <p>And this is the line where it fails..</p>
  
  <pre><code>final List currentResults = loaders[i].list(this);
  </code></pre>
  
  <p>and this line further calls this method and fails,</p>
  
  <pre><code>public List list(SessionImplementor session) 
      throws HibernateException {
          return list( session, translator.getQueryParameters(), querySpaces, resultTypes );
  
      }
  </code></pre>
  
  <p>I have all my Dao's <code>autowired</code> in my controller like,  </p>
  
  <pre><code>@Autowired
  private SortCodeDao sortCodeDao;
  </code></pre>
  
  <p>Using spring MVC, Hibernate in my application. using criteriaImpl, before returning when it comes here </p>
  
  <pre><code>DomLoadKey result = (DomLoadKey) c.uniqueResult();
  </code></pre>
  
  <p>it is giving the exception. I have similar method to </p>
  
  <pre><code>IntlLoadKey
  </code></pre>
  
  <p>which is working. But when it comes to Dom its failing..</p>
  
  <p>Following is my stacktrace..</p>
  
  <pre><code>org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ca.ups.tle.sortCode.SortCode
      at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
      at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
      at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:130)
      at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1873)
      at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1844)
      at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716)
      at org.hibernate.loader.Loader.doQuery(Loader.java:801)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
      at org.hibernate.loader.Loader.doList(Loader.java:2542)
      at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
      at org.hibernate.loader.Loader.list(Loader.java:2271)
      at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
      at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
      at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
      at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:369)
      at ca.ups.tle.loadKey.DomLoadKeyHbmDao.read(DomLoadKeyHbmDao.java:74)
      at ca.ups.tle.loadKey.DomLoadKeyHbmDao$$FastClassByCGLIB$$3036def2.invoke(&lt;generated&gt;)
      at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
      at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
      at ca.ups.tle.loadKey.DomLoadKeyHbmDao$$EnhancerByCGLIB$$71e33677_2.read(&lt;generated&gt;)
      at ca.ups.tle.web.controller.assignLoads.NISLoadEntryController.usePositionInformationNis(NISLoadEntryController.java:1010)
      at ca.ups.tle.web.controller.assignLoads.NISLoadEntryController$$FastClassByCGLIB$$c0ff7011.invoke(&lt;generated&gt;)
      at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
      at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
      at ca.ups.tle.web.controller.assignLoads.NISLoadEntryController$$EnhancerByCGLIB$$64e555c_2.usePositionInformationNis(&lt;generated&gt;)
      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.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:58)
      at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:104)
      at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57)
      at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
      at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:97)
      at org.springframework.binding.expression.spel.SpringELExpression.getValue(SpringELExpression.java:84)
      at org.springframework.webflow.action.EvaluateAction.doExecute(EvaluateAction.java:75)
      at org.springframework.webflow.action.AbstractAction.execute(AbstractAction.java:188)
      at org.springframework.webflow.execution.AnnotatedAction.execute(AnnotatedAction.java:145)
      at org.springframework.webflow.execution.ActionExecutor.execute(ActionExecutor.java:51)
      at org.springframework.webflow.engine.support.ActionTransitionCriteria.test(ActionTransitionCriteria.java:82)
      at org.springframework.webflow.engine.support.TransitionCriteriaChain.test(TransitionCriteriaChain.java:71)
      at org.springframework.webflow.engine.Transition.canExecute(Transition.java:195)
      at org.springframework.webflow.engine.Transition.execute(Transition.java:211)
      at org.springframework.webflow.engine.impl.FlowExecutionImpl.execute(FlowExecutionImpl.java:393)
      at org.springframework.webflow.engine.impl.RequestControlContextImpl.execute(RequestControlContextImpl.java:214)
      at org.springframework.webflow.engine.TransitionableState.handleEvent(TransitionableState.java:119)
      at org.springframework.webflow.engine.Flow.handleEvent(Flow.java:555)
      at org.springframework.webflow.engine.impl.FlowExecutionImpl.handleEvent(FlowExecutionImpl.java:388)
      at org.springframework.webflow.engine.impl.RequestControlContextImpl.handleEvent(RequestControlContextImpl.java:210)
      at org.springframework.webflow.engine.ViewState.handleEvent(ViewState.java:232)
      at org.springframework.webflow.engine.ViewState.resume(ViewState.java:196)
      at org.springframework.webflow.engine.Flow.resume(Flow.java:545)
      at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:261)
      at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
      at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:183)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>