title: org.hibernate.TransientObjectException during Criteria.list()
body: <p>I have seen posts all over the internet that talk about how to fix the TransientObjectExceptions during save/update/delete but I am having this problem when calling list on my Criteria.</p>
  
  <p>I have two objects A and B.  A has a field named b which is of type B.  In my mapping b is mapped as a many-to-one.  This all runs in a larger persistence framework (the framework is kind of like Core Data) and so I don't use any cascades in my hibernate mappings since cascades are handled at a higher level.</p>
  
  <p>This is the interesting code surrounding my criteria:</p>
  
  <pre><code>A a = new A();
  B b = new B();
  a.setB(b);
  
  session.save("B", b); // Actually handled by the higher level
  session.save("A", a); // framework, this is just for clarity
  
  // transaction committed and session closed
  ...
  // new session opened
  
  Criteria criteria = session.createCriteria(A.class);
  criteria.add(Restrictions.eq("b", b));
  List&lt;?&gt; objects = criteria.list();
  </code></pre>
  
  <p>Basically I am looking for all objects of type A such that A.b equals a particular instance of b (I actually tried restructuring a query so that I was passing in the id of b just to make sure that b wasn't causing me problems).</p>
  
  <p>Here is the stack trace that occurs when I call criteria.list():</p>
  
  <pre><code>org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: B
  at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:244)
  at org.hibernate.type.EntityType.getIdentifier(EntityType.java:449)
  at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:141)
  at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1769)
  at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1740)
  at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
  at org.hibernate.loader.Loader.doQuery(Loader.java:717)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
  at org.hibernate.loader.Loader.doList(Loader.java:2294)
  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
  at org.hibernate.loader.Loader.list(Loader.java:2167)
  at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
  at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
  </code></pre>
  
  <p>Here is my mapping:</p>
  
  <blockquote>
    <p><code>&lt;class entity-name="A" lazy="false"&gt;</code><br>
        <code>&lt;tuplizer entity-mode="dynamic-map" class="MyTuplizer" /&gt;</code><br>
        <code>&lt;id type="long" column="id"&gt;</code><br>
            <code>&lt;generator class="native" /&gt;</code><br>
        <code>&lt;/id&gt;</code><br>
        <code>&lt;many-to-one name="b" entity-name="B" column="b_id" lazy="false" /&gt;</code><br>
        <code>&lt;/class&gt;</code><br>
    <br>
        <code>&lt;class entity-name="B" lazy="false"&gt;</code><br>
        <code>&lt;tuplizer entity-mode="dynamic-map" class="MyTuplizer" /&gt;</code><br>
        <code>&lt;id type="long" column="id"&gt;</code><br>
        <code>&lt;generator class="native" /&gt;</code><br>
        <code>&lt;/id&gt;</code><br>
        <code>&lt;/class&gt;</code>  </p>
  </blockquote>
  
  <p>Can anyone help me figure out why I would be getting a TransientObjectException during a fetch?  Preferably I would like to find a solution that does not rely on cascades since they tend to mask problems that occur in the higher level framework.</p>