Non-entity superclasses with XML mappings creates incorrect schema
Description
Activity

Christian Beikov December 10, 2021 at 9:49 AM
I’ll close this issue as I provided you citation from the spec that supports/permits the current behavior and you haven’t responded yet. You know the procedure, I’ll reopen in case you think this is still an issue . After all, Hibernate is passing the JPA TCK, so if there is anything to do here, it would be adding a test to the TCK that asserts whatever behavior you think can be deduced from the spec text. Like I outlined through citation though, and I assume other JPA spec committers will come to the same conclusion, the current behavior is AFAIU supported/permitted.

Christian Beikov November 16, 2021 at 8:09 AM
Let me cite the specification 12.1.:
If the xml-mapping-metadata-complete subelement is specified, the complete set of mapping metadata for the persistence unit is contained in the XML mapping files for the persistence unit, and any persistence annotations on the classes are ignored.
If xml-mapping-metadata-complete is specified and XML elements are omitted, the default values apply. These default values are the same as the corresponding defaults when annotations are used, except in the cases specified in Section 12.2 below. When the xml-mapping-metadata-complete element is specified, any metadata-complete attributes specified within the entity, mapped-superclass, and embeddable elements are ignored.
So from this it follows that not specifying this element will cause the default values to apply, just like the second section I cited says. Now you might think that because there are no annotations on the properties, that “the annotation perspective” is that the class has no properties, but this is AFIAU not correct. Every property/field (depending on the access type) has implicit defaults as per 2.8, even if it doesn’t have annotations.
If a persistent field or property other than a relationship property is not annotated with one of the mapping annotations defined in Chapter 11 (or equivalent mapping information is not specified in the XML descriptor), the following default mapping rules are applied in order:
• If the type is a class that is annotated with the Embeddable annotation, it is mapped in the same way as if the field or property were annotated with the Embedded annotation. See Section 11.1.15 and Section 11.1.16.
• If the type of the field or property is one of the following, it is mapped in the same way as it would if it were annotated as Basic: Java primitive types, wrappers of the primitive types, java.lang.String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime, java.time.OffsetTime, java.time.OffsetDateTime, byte[], Byte[], char[], Character[], enums, any other type that implements Serializable. See Section 11.1.6, Section 11.1.18, Section 11.1.28, and Section 11.1.53.
It is an error if no annotation is present and none of the above rules apply
I’m also actually pretty sure that the TCK asserts this interpretation, which is, XML mapping is by default an override to the implicit mapping defaults. So using <entity class=”...”>
within the orm.xml is like annotating the class with @Entity
, which I hope you agree is bound to this implicit mapping defaults.
So what is happening from my POV for your model is that, because you are missing the xml-mapping-metadata-complete
element, the properties/fields are interpreted as per 2.8 and the XML mapping is treated as override.
Now, if you could please commit the persistence.xml change, we could observe what Hibernate does with a mapping that meets your needs according to the JPA spec and if it doesn’t, I will take a deeper look.
Will Dazey November 15, 2021 at 3:54 PMEdited
I don’t agree that xml-mapping-metadata-complete
is required by the specification to use XML mapping. I’m not quite sure why you keep thinking that property is required/needed here.
The only purpose of the xml-mapping-metadata-complete
configuration is to inform the provider that XML relational mapping descriptors should override any annotation values. When xml-mapping-metadata-complete
is used, any persistence annotations on the classes are ignored.
However, there are no annotations in this usecase. There are no annotations to ignore. Why would a user read section 12.1, look at their XML only configuration, and come to the conclusion that xml-mapping-metadata-complete
is required by Hibernate?
Also, setting xml-mapping-metadata-complete
has no visible change to behavior

Christian Beikov November 15, 2021 at 12:17 PM
Ok, so could you please also add <persistence-unit-metadata><xml-mapping-metadata-complete/></persistence-unit-metadata>
as that is required by the specification to instruct the JPA provider to treat the XML as “complete mapping” and not as “additional mapping”.
Will Dazey November 14, 2021 at 8:03 PM
Thank you! I wasn’t quite sure how to test to validate teh schema generation, but the native query suggestion was a good idea. I have updated to include that strategy and I see it is failing with the expected error.
I was testing Hibernate's behavior with non-entity superclasses as described in section 2.11.3 of the JPA specification.
However, I am seeing a difference in behavior between the annotation version and the equivalent XML mapping version. Hibernate generates incorrect schemas for XML mapped classes; including the non-entity superclass fields in the XML mapped entity table. First, I will document the annotation usecase and Hibernates behavior.
— Annotation Usecase —
Hibernate generated schema:
Notice how the non-entity superclass `AbstractPKEntity` declares no fields and only contains "getter/setter" methods. These should ultimately be ignored as business logic and not be reflected in any entity subclasses. I am seeing in the annotation example that the entity PKEntityLong table is only generating with the columns listed in the entity class. I believe this is a correct schema generation.
— XML Mapping Usecase —
orm.xml:
Hibernate generated schema:
Notice that with XML mapping, the schema is generating with an additional column `longPK` even though the XML configuration lists no such field. Is Hibernate processing the `XMLPKEntityLong` class using property access and assuming getLongPK()/setLongPK() refer to unlisted fields? That is unwarranted, incorrect according to the specification, and inconsistent with the annotation usecase.