Samuel Rimbert created OPENJPA-3107:
---------------------------------------

             Summary: Static metamodel generated by AnnotationProcessor6 in 
4.2.0 is rejected at runtime: "class_" field has less than two type parameters
                 Key: OPENJPA-3107
                 URL: https://issues.apache.org/jira/browse/OPENJPA-3107
             Project: OpenJPA
          Issue Type: Bug
         Environment: OpenJPA 4.2.0, JDK 21.0.8, jakarta.persistence-api 3.2.0, 
Spring Framework 7 / Spring ORM (LocalContainerEntityManagerFactoryBean), H2 in 
MySQL mode.
            Reporter: Samuel Rimbert


OpenJPA 4.2.0 generates the JPA 3.2 "class_" constant in every canonical 
metamodel class, but the 4.2.0 runtime (MetamodelImpl) does not accept that 
field. As a result, any persistence unit whose entities have a generated static 
metamodel fails as soon as the metamodel is populated (e.g. on the first 
EntityManager#getCriteriaBuilder() / getMetamodel()).

Generator side - 
org.apache.openjpa.persistence.meta.AnnotationProcessor6#addManagedTypeConstant:

    SourceCode.Field classField = modelClass.addField("class_", typeClass);   
// EntityType / MappedSuperclassType / EmbeddableType
    classField.addParameter(originalSimpleClass);
    classField.makePublic().makeStatic().makeVolatile();

which produces, for an entity Foo:

    public static volatile jakarta.persistence.metamodel.EntityType<Foo> class_;

Runtime side - org.apache.openjpa.persistence.meta.MetamodelImpl:
- populate(AbstractManagedType) (line 367) iterates over all declared fields of 
the metamodel class and calls getParameterizedType(mf) for each;
- getParameterizedType(Field) (line 420) throws for any parameterized field 
with fewer than two type arguments:

    if (args.length < 2) {
        throw new IllegalStateException(_loc.get("meta-field-less-param", ...));
    }

EntityType<X> / MappedSuperclassType<X> / EmbeddableType<X> have a single type 
argument, so "class_" always hits this branch:

    Encountered field "jakarta.persistence.metamodel.EntityType<Foo> class_" in 
"class Foo_".
    This field has less than two parameters and is not recognized as a 
canonical field.

Even if that check were relaxed, populate() would then call 
type.getAttribute("class_"), which is not an attribute. The "class_" field (JPA 
3.2, section 6.2.1.1) should instead be skipped by the attribute loop and 
assigned the ManagedType itself (mf.set(null, type)).

Secondary issue: in populate(), the catch block builds its message with 
f.getJavaType() while f may still be null (or hold the previous field's 
attribute), which can hide the original exception behind a NullPointerException 
or a misleading "meta-field-mismatch" message.

Steps to reproduce:
1. Any entity, e.g.
       @Entity public class Foo \{ @Id private long id; private String name; }
2. Generate the metamodel with OpenJPA 4.2.0 (maven-processor-plugin or javac 
-processor org.apache.openjpa.persistence.meta.AnnotationProcessor6 
-Aopenjpa.metamodel=true).
3. Run with OpenJPA 4.2.0 and the generated Foo_ on the classpath, then call 
em.getCriteriaBuilder() (or em.getMetamodel().entity(Foo.class)).
4. IllegalStateException "meta-field-less-param" on Foo_.class_.

Expected: metamodel populated, Foo_.class_ set to the EntityType<Foo>, 
Foo_.name set to the SingularAttribute.

Observed impact: on a real application (~220 entities, Java 21, Spring 7) every 
test touching the Criteria API or named queries fails with 4.2.0, while the 
same code and test bench pass with 4.1.1 (metamodel generated by 4.1.1, which 
has no "class_" field). No workaround found other than staying on 4.1.1: the 
4.2.0 processor has no option to skip "class_", and we could not get a 
4.1.1-generated metamodel to build cleanly against the 4.2.0 artifacts.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to