title: JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException
body: <p>I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy:</p>
  
  <pre><code>...
  Root&lt;JPAAlbum&gt; albm = cq.from(JPAAlbum.class);
  ... albm.get(JPAAlbum_.theme) ... ;
  </code></pre>
  
  <p>but this Root.get always throws a <code>NullPointerException</code>. <code>JPAAlbum_.theme</code> was automatically generated by Hibernate and looks like</p>
  
  <pre><code>public static volatile SingularAttribute&lt;JPAAlbum, JPATheme&gt; theme;
  </code></pre>
  
  <p>but it's obviously never populated.</p>
  
  <p>Am I missing a step in the initialization of the framework ?</p>
  
  <p><strong>EDIT:</strong> here is a snippet of how I use JPA and the metamodel when it's crashing:</p>
  
  <pre><code>    CriteriaBuilder cb = em.getCriteriaBuilder();
  
      CriteriaQuery&lt;JPAAlbum&gt; cq = cb.createQuery(JPAAlbum.class) ;
      Root&lt;JPAAlbum&gt; albm = cq.from(JPAAlbum.class);
      cq.where(cb.equal(albm.get(JPAAlbum_.theme).get(JPATheme_.id),
                          session.getTheme().getId())) ;
  </code></pre>
  
  <p>(<code>JPAAlbum_</code> is a class, so I just <code>import</code> before) and the associated stacktrace:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
      at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:138)
      at net.wazari.dao.jpa.WebAlbumsDAOBean.getRestrictionToAlbumsAllowed(WebAlbumsDAOBean.java:55)
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>In the JBoss EntityManager guide, I can see that</p>
  
  <blockquote>
    <p>When the Hibernate EntityManagerFactory is being built, it will look for a canonical metamodel class for each of the managed typed is knows about and if it finds any it will inject the appropriate metamodel information into them, as outlined in [JPA 2 Specification, section 6.2.2, pg 200]</p>
  </blockquote>
  
  <p>I could also verify with </p>
  
  <pre><code>     for (ManagedType o : em.getMetamodel().getManagedTypes()) {
              log.warn("___") ;
              for (Object p : o.getAttributes()) {
                  log.warn(((Attribute)p).getName()) ;
              }
          }
  </code></pre>
  
  <p>that Hibernate is aware of my metamodel, the attribute names are written, however </p>
  
  <pre><code>   log.warn("_+_"+JPAPhoto_.id+"_+_") ;
  </code></pre>
  
  <p>remains desperately empty ...</p>
  
  <p><strong>EDIT3</strong>: here is the <a href="http://www.assembla.com/code/WebAlbums/subversion/nodes/WebAlbums3/trunk/WebAlbums3-DAO-JPABeans/src/java/net/wazari/dao/jpa/entity/JPAAlbum.java?rev=238" rel="noreferrer">JPAAlbum entity</a> and its <a href="http://www.assembla.com/code/WebAlbums/subversion/nodes/WebAlbums3/trunk/WebAlbums3-DAO-JPABeans/src/java/net/wazari/dao/jpa/entity/metamodel/JPAAlbum_.java?rev=238" rel="noreferrer">metamodel class</a>.</p>
  
  <p>What else can I tell about my configuration ... </p>
  
  <ul>
  <li><p>I use Hibernat <em>3.5.6-Final</em> (according to META-INF/MANIFEST.MF), </p></li>
  <li><p>deploy on Glassfish <em>3.0.1</em> </p></li>
  <li><p>from Netbeans <em>6.9.1</em>; </p></li>
  <li><p>and the application relies on EJB <em>3.1</em>,</p></li>
  </ul>
  
  <p>I hope it will help !</p>
  
  <p><strong>EDIT 4:</strong></p>
  
  <p>unfortunately, the JUnit test leads to the same exception:</p>
  
  <pre><code>java.lang.NullPointerException
      at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:138)
      at net.wazari.dao.test.TestMetaModel.foo(TestMetaModel.java:55)
  </code></pre>
  
  <p><strong>A much simpler project is available <a href="http://www.assembla.com/code/WebAlbums/subversion/nodes/WebAlbums3/trunk/EJBModule1?rev=295" rel="noreferrer">here</a>/<a href="https://www.assembla.com/code/WebAlbums/subversion/nodes/WebAlbums3/trunk/bug/EJBModule1.tar?rev=297" rel="noreferrer">tarball</a>.</strong> It only contains my entities and their metamodel, plus a JUnit test (foo crashes with metamodel, bar is okay with the usual Query.</p>
  
  <p><strong>EDIT 5:</strong></p>
  
  <p>You should be able to reproduce the problem by downloading the <a href="https://www.assembla.com/code/WebAlbums/subversion/nodes/WebAlbums3/trunk/bug/EJBModule1.tar?rev=297" rel="noreferrer">tarball</a>, building the project:</p>
  
  <pre><code>ant compile
  or
  ant dist
  </code></pre>
  
  <p>and start the JUnit test <code>net.wazari.dao.test.TestMetaModel</code></p>
  
  <pre><code> CLASSPATH=`sh runTest.sh` java org.junit.runner.JUnitCore  net.wazari.dao.test.TestMetaModel
  </code></pre>
  
  <p>(edit <code>runTest.sh</code> to point CLASSPATH to the right location of your JUnit4-5 jar)</p>
  
  <p>All the hibernate dependencies I use should be included in the archive.</p>