JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException

Kevin picture Kevin · Oct 4, 2010 · Viewed 20.4k times · Source

I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy:

...
Root<JPAAlbum> albm = cq.from(JPAAlbum.class);
... albm.get(JPAAlbum_.theme) ... ;

but this Root.get always throws a NullPointerException. JPAAlbum_.theme was automatically generated by Hibernate and looks like

public static volatile SingularAttribute<JPAAlbum, JPATheme> theme;

but it's obviously never populated.

Am I missing a step in the initialization of the framework ?

EDIT: here is a snippet of how I use JPA and the metamodel when it's crashing:

    CriteriaBuilder cb = em.getCriteriaBuilder();

    CriteriaQuery<JPAAlbum> cq = cb.createQuery(JPAAlbum.class) ;
    Root<JPAAlbum> albm = cq.from(JPAAlbum.class);
    cq.where(cb.equal(albm.get(JPAAlbum_.theme).get(JPATheme_.id),
                        session.getTheme().getId())) ;

(JPAAlbum_ is a class, so I just import before) and the associated stacktrace:

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)

EDIT 2:

In the JBoss EntityManager guide, I can see that

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]

I could also verify with

     for (ManagedType o : em.getMetamodel().getManagedTypes()) {
            log.warn("___") ;
            for (Object p : o.getAttributes()) {
                log.warn(((Attribute)p).getName()) ;
            }
        }

that Hibernate is aware of my metamodel, the attribute names are written, however

   log.warn("_+_"+JPAPhoto_.id+"_+_") ;

remains desperately empty ...

EDIT3: here is the JPAAlbum entity and its metamodel class.

What else can I tell about my configuration ...

  • I use Hibernat 3.5.6-Final (according to META-INF/MANIFEST.MF),

  • deploy on Glassfish 3.0.1

  • from Netbeans 6.9.1;

  • and the application relies on EJB 3.1,

I hope it will help !

EDIT 4:

unfortunately, the JUnit test leads to the same exception:

java.lang.NullPointerException
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:138)
    at net.wazari.dao.test.TestMetaModel.foo(TestMetaModel.java:55)

A much simpler project is available here/tarball. It only contains my entities and their metamodel, plus a JUnit test (foo crashes with metamodel, bar is okay with the usual Query.

EDIT 5:

You should be able to reproduce the problem by downloading the tarball, building the project:

ant compile
or
ant dist

and start the JUnit test net.wazari.dao.test.TestMetaModel

 CLASSPATH=`sh runTest.sh` java org.junit.runner.JUnitCore  net.wazari.dao.test.TestMetaModel

(edit runTest.sh to point CLASSPATH to the right location of your JUnit4-5 jar)

All the hibernate dependencies I use should be included in the archive.

Answer

debbie picture debbie · Feb 3, 2011

I had the same issue and it was fixed by putting the Model and Model_ class into the same package.