javahibernatejpainner-classes

Is it possible to use constructor expression for nested class in JPA?


The following code

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) {}
    }
}

throw exception during application initialization:

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

However if the B class is moved up to be standalone class p.B, it works ok.

Is it possible to use nested classes with JPA constructor expressions?

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


Solution

  • Use $ instead of . as a class separator in qualified name in the query:

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

    See Hibernate bug.