title: JPA managed entity throwing null pointer exception
body: <p>I am using JPA 2.0 along with SpringBoot for one of my projects. Since the application required lots of read-only queries, I decided to make a singleton (Autowired) class with entityManager marked as 
  <strong>@PersistenceContext(type=PersistenceContextType.EXTENDED)</strong>. </p>
  
  <p>This same instance of the extended entity manager was in turn used by the numerous DAOs (perhaps in multiple threads) for their read only requests. Since the entities used to be within the extended persistence context, the calls to further elaborate (hydrate, if you will) on the entity never gave LazyInitializationException. Also, since the entities, once loaded, were always present in the active memory of the extended persistence context one used to get excellent performance. </p>
  
  <p>However, with passing time I occasionally started getting <strong>NullPointerException</strong> while accessing one of the fields of one of the managed entities. The behaviour was non-deterministic. </p>
  
  <pre><code>        // .. earlier code skipped for brevity
          Child child = refNode.getFirstChild();
  
          if (child != null) {
              // Print confirms that the object is managed. In fact, earlier merely 
              // inserting print statement would dissolve the error.
              System.out.printf ("Child ID = %s %s\n", child.getChildID(), childDAO.getContext(child));
  
              // NPE while accessing latestVersion attribute of 'Child'.
              // Values does exist in DB. 
              Unode un = child.getLatestVersion().getuNode();
              return un;
          }
  </code></pre>
  
  <p>The relevant portion of 'Child' entity is as follows:</p>
  
  <pre><code>    public class Child implements Serializable { 
          private static final long serialVersionUID = 1L;
  
          @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
          private int childID;
          ...
          @ManyToOne(cascade={CascadeType.ALL})
          @JoinColumn(name="latestVersionID")
          private Version latestVersion;
  </code></pre>
  
  <p>And yes, I also received the following error quite generously.</p>
  
  <pre><code>    HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: possible non-threadsafe access to the session
  </code></pre>
  
  <p>Of late I am also getting 'ConcurrentModificationException', unsafe thread accesss, and similar errors in all sort of hitherto clean portions of the code. I also tried making my extended context entity manager thread safe using synchronized(mutex) at all access, but of no help.</p>
  
  <p>My biggest worry is a managed entity (in extended context) giving null pointed exception. Though the exceptions appear non-deterministically they appear to be often centered around places where 'Child' or 'Child.LatestVersion' is accessed.</p>
  
  <p>I tried, Lazy Loading, Eager Loading etc., but of no use. My understanding (upto this point) was that forward entities are loaded eagerly, reverse entities lazily, and never a null pointed exception when the entity is managed (in whichever context - extended or transactional). </p>
  
  <p>Any help would be sincerely appreciated. </p>