是否可以在JPA中为嵌套类使用构造函数表达式?

时间:2015-05-30 23:29:11

标签: java hibernate jpa nested-class

以下代码

package p

@Entity
@NamedQueries({
    @NamedQuery(name = "A.findAllB"
                query = "SELECT new p.A.B(a.bId, a.bName) FROM A a")
})
public class A implements Serializable {

    public static class B {

         public B(long id, String name) {}
    }
}

在应用程序初始化期间抛出异常:

Caused by: org.hibernate.HibernateException: Errors in named queries: A.findAllB
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:545) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) [jipijapa-hibernate4-3-1.0.1.Final.jar:]
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
... 8 more

但是,如果将B类提升为独立类p.B,则可以正常运行。

是否可以在JPA构造函数表达式中使用嵌套类?

环境: Java 8,Java EE 7,WildFly 8.2,hibernate-jpa-api 1.0.0

1 个答案:

答案 0 :(得分:2)

使用$代替.作为查询中限定名称的类分隔符:

"SELECT new p.A$B(a.bId, a.bName) FROM A a"

请参阅Hibernate bug