title: NullPointerException in Hibernate for fetch = EAGER
body: <p>I've been trying to solve a problem with a NullPointerException in a Hibernate class when I set the fetch attribute of a @OneToMany field to FetchType.EAGER. I assume I'm doing something wrong but I can't see what, can anyone help?</p>
  
  <p>This is the error:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
      at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:780)
      at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:58)
      at org.hibernate.event.spi.InitializeCollectionEvent.&lt;init&gt;(InitializeCollectionEvent.java:22)
      at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2002)
      at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:562)
      at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:246)
      at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:558)
      at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:131)
      at org.hibernate.collection.internal.PersistentSet.toArray(PersistentSet.java:170)
      at testhib.entities.Parent.$$_hibernate_write_children(Parent.java)
      ... 64 more
  </code></pre>
  
  <p>This is the test case I've constructed:</p>
  
  <p>Database</p>
  
  <pre><code>CREATE TABLE `test`.`parent` (
    `parent_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `parent_version` int(10) unsigned NOT NULL DEFAULT '1',
    `parent_name` varchar(64) DEFAULT NULL,
    PRIMARY KEY (`parent_id`)
  );
  
  CREATE TABLE `test`.`child` (
    `child_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `child_version` int(10) unsigned NOT NULL DEFAULT '1',
    `child_name` varchar(64) DEFAULT NULL,
    `child_parent` int(10) unsigned NOT NULL,
    PRIMARY KEY (`child_id`),
    KEY `fk_child_1_idx` (`child_parent`),
    CONSTRAINT `fk_child_1` FOREIGN KEY (`child_parent`) REFERENCES `parent` (`parent_id`)
  );
  
  INSERT INTO `test`.`parent`
  (`parent_id`, `parent_name`)
  VALUES
  (1, 'Homer');
  
  INSERT INTO `test`.`child`
  (`child_id`, `child_name`, `child_parent`)
  VALUES
  (1, 'Bart', 1),
  (2, 'Lisa', 1),
  (3, 'Maggie', 1);
  </code></pre>
  
  <p>Entities</p>
  
  <pre><code>package testhib.entities;
  
  import java.io.Serializable;
  import java.util.Set;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  import static javax.persistence.FetchType.EAGER;
  
  @Entity
  @Table(name = "parent")
  public class Parent implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "parent_id")
      private Integer parentId;
  
      @Basic(optional = false)
      @Column(name = "parent_version")
      private int parentVersion;
  
      @Column(name = "parent_name")
      private String parentName;
  
      @OneToMany(mappedBy = "parent", fetch = EAGER)
      private Set&lt;Child&gt; children;
  
      public Integer getParentId() {
          return parentId;
      }
  
      public void setParentId(Integer parentId) {
          this.parentId = parentId;
      }
  
      public int getParentVersion() {
          return parentVersion;
      }
  
      public void setParentVersion(int parentVersion) {
          this.parentVersion = parentVersion;
      }
  
      public String getParentName() {
          return parentName;
      }
  
      public void setParentName(String parentName) {
          this.parentName = parentName;
      }
  
      public Set&lt;Child&gt; getChildren() {
          return children;
      }
  
      public void setChildren(Set&lt;Child&gt; children) {
          this.children = children;
      }
  }
  
  package testhib.entities;
  
  import java.io.Serializable;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "child")
  public class Child implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "child_id")
      private Integer childId;
  
      @Basic(optional = false)
      @Column(name = "child_version")
      private int childVersion;
  
      @Column(name = "child_name")
      private String childName;
  
      @JoinColumn(name = "child_parent", referencedColumnName = "parent_id")
      @ManyToOne
      private Parent parent;
  
      public Integer getChildId() {
          return childId;
      }
  
      public void setChildId(Integer childId) {
          this.childId = childId;
      }
  
      public int getChildVersion() {
          return childVersion;
      }
  
      public void setChildVersion(int childVersion) {
          this.childVersion = childVersion;
      }
  
      public String getChildName() {
          return childName;
      }
  
      public void setChildName(String childName) {
          this.childName = childName;
      }
  
      public Parent getParent() {
          return parent;
      }
  
      public void setParent(Parent parent) {
          this.parent = parent;
      }
  }
  </code></pre>
  
  <p>Test</p>
  
  <pre><code>package testhib.test;
  
  import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  import org.junit.After;
  import org.junit.AfterClass;
  import org.junit.Before;
  import org.junit.BeforeClass;
  import org.junit.Test;
  import testhib.entities.Parent;
  
  import static org.junit.Assert.assertThat;
  import static org.hamcrest.Matchers.containsInAnyOrder;
  
  public class Tests {
  
      private static EntityManagerFactory emf;
      private EntityManager em;
  
      public Tests() {
      }
  
      @BeforeClass
      public static void setUpClass() {
  
          emf = Persistence.createEntityManagerFactory("test-hib");
      }
  
      @AfterClass
      public static void tearDownClass() {
  
          emf.close();
      }
  
      @Before
      public void setUp() {
  
          em = emf.createEntityManager();
  
          em.getTransaction().begin();
      }
  
      @After
      public void tearDown() {
  
          em.getTransaction().commit();
  
          em.close();
      }
  
      @Test
      public void canRead() {
  
          Parent p = em.find(Parent.class, 1);
  
          Set&lt;String&gt; names = new HashSet&lt;&gt;();
  
          p.getChildren().stream().forEach((c) -&gt; {
  
              names.add(c.getChildName());
          });
  
          assertThat(names, containsInAnyOrder("Bart", "Lisa", "Maggie"));
      }
  }
  </code></pre>
  
  <p>peristence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
    &lt;persistence-unit name="test-hib" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;class&gt;testhib.entities.Parent&lt;/class&gt;
      &lt;class&gt;testhib.entities.Child&lt;/class&gt;
      &lt;properties&gt;
        &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test"/&gt;
        &lt;property name="javax.persistence.jdbc.user" value="test"/&gt;
        &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
        &lt;property name="javax.persistence.jdbc.password" value="password"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>POM</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project
          xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;uk.co.discoverdorset&lt;/groupId&gt;
      &lt;artifactId&gt;test-hib&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;test-hib&lt;/name&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;5.1.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.39&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.12&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
              &lt;artifactId&gt;hamcrest-all&lt;/artifactId&gt;
              &lt;version&gt;1.3&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
              &lt;version&gt;7.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.hibernate.orm.tooling&lt;/groupId&gt;
                  &lt;artifactId&gt;hibernate-enhance-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;5.1.0.Final&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;configuration&gt;
                              &lt;failOnError&gt;true&lt;/failOnError&gt;
                              &lt;enableLazyInitialization&gt;true&lt;/enableLazyInitialization&gt;
                              &lt;enableDirtyTracking&gt;true&lt;/enableDirtyTracking&gt;
                              &lt;enableAssociationManagement&gt;true&lt;/enableAssociationManagement&gt;
                              &lt;enableExtendedEnhancement&gt;false&lt;/enableExtendedEnhancement&gt;
                          &lt;/configuration&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;enhance&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>The test runs without a hitch if I comment out the "fetch = EAGER" from the Parent entity.</p>
  
  <p>Any ideas?</p>