JPA中的嵌套类可以使用构造函数表达式吗?

问题描述 投票:0回答:1

以下代码

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

java hibernate jpa inner-classes
1个回答
7
投票

在查询中使用

$
而不是
.
作为限定名称中的类分隔符:

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

请参阅休眠错误

© www.soinside.com 2019 - 2024. All rights reserved.