未填充 JPA/Hibernate 静态元模型属性 -- NullPointerException [英] JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException
问题描述
我想将 JPA2 Criteria API 与元模型对象一起使用,这似乎很简单:
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) ... ;
但是这个 Root.get 总是抛出一个 NullPointerException
.JPAAlbum_.theme
是由 Hibernate 自动生成的,看起来像
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 ?
这里是我在崩溃时如何使用 JPA 和元模型的片段:
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_
是一个类,所以我之前只是import
)和相关的堆栈跟踪:
(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)
编辑 2:
在 JBoss EntityManager 指南中,我可以看到
In the JBoss EntityManager guide, I can see that
当 Hibernate EntityManagerFactory 被构建时,它会为每个托管类型的知道的标准元模型类寻找一个规范的元模型类,如果找到的话,它会将适当的元模型信息注入到它们中,如 [JPA 2 规范中所述,第 6.2.2 节,第 200 页]
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]
我也可以用
for (ManagedType o : em.getMetamodel().getManagedTypes()) {
log.warn("___") ;
for (Object p : o.getAttributes()) {
log.warn(((Attribute)p).getName()) ;
}
}
Hibernate 知道我的元模型,但写入了属性名称
that Hibernate is aware of my metamodel, the attribute names are written, however
log.warn("_+_"+JPAPhoto_.id+"_+_") ;
空荡荡的......
EDIT3:这里是 JPAAlbum 实体 及其元模型类.
关于我的配置我还能说些什么...
What else can I tell about my configuration ...
我使用 Hibernat 3.5.6-Final(根据 META-INF/MANIFEST.MF),
I use Hibernat 3.5.6-Final (according to META-INF/MANIFEST.MF),
部署在 Glassfish 3.0.1
deploy on Glassfish 3.0.1
来自 Netbeans 6.9.1;
from Netbeans 6.9.1;
并且应用程序依赖于 EJB 3.1,
and the application relies on EJB 3.1,
希望能帮到你!
编辑 4:
不幸的是,JUnit 测试导致了同样的异常:
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)
一个更简单的项目可用 这里/tarball.它只包含我的实体和它们的元模型,加上一个 JUnit 测试(foo 与元模型崩溃,bar 与通常的查询没问题.
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.
编辑 5:
您应该能够通过下载 tarball,构建项目:
You should be able to reproduce the problem by downloading the tarball, building the project:
ant compile
or
ant dist
并开始JUnit测试net.wazari.dao.test.TestMetaModel
CLASSPATH=`sh runTest.sh` java org.junit.runner.JUnitCore net.wazari.dao.test.TestMetaModel
(编辑 runTest.sh
以将 CLASSPATH 指向 JUnit4-5 jar 的正确位置)
(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.
推荐答案
我遇到了同样的问题,通过将 Model
和 Model_
类放到同一个包.
I had the same issue and it was fixed by putting the Model
and Model_
class into the same package.
这篇关于未填充 JPA/Hibernate 静态元模型属性 -- NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!