是否可以在 JPA 中对嵌套 class 使用构造函数表达式?
Is is possible to use constructor expression for nested class in JPA?
以下代码
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.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.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.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
... 8 more
然而,如果 B
class 向上移动为独立的 class p.B
,它工作正常。
是否可以将嵌套的 classes 与 JPA 构造函数表达式一起使用?
环境:
Java8,JavaEE 7,WildFly 8.2,hibernate-jpa-api1.0.0
在查询的限定名称中使用 $
而不是 .
作为 class 分隔符:
"SELECT new p.A$B(a.bId, a.bName) FROM A a"
以下代码
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.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.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.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
... 8 more
然而,如果 B
class 向上移动为独立的 class p.B
,它工作正常。
是否可以将嵌套的 classes 与 JPA 构造函数表达式一起使用?
环境: Java8,JavaEE 7,WildFly 8.2,hibernate-jpa-api1.0.0
在查询的限定名称中使用 $
而不是 .
作为 class 分隔符:
"SELECT new p.A$B(a.bId, a.bName) FROM A a"