Mapping Embeddable to Secondary Table with XML mapping creates incorrect schema

Description

I was testing Hibernate's behavior with Secondary Tables and Embeddables and noticed a difference in behavior between the annotation version and the equivalent XML mapping version. First, I will document the annotation usecase and Hibernates behavior.

— Annotation Usecase —

Hibernate generated schema:

Notice how the primary table `AnnEmbedMultiTableEnt` does not contain the Embeddable values as they have been mapped to the Secondary Table via `@Column(table = "SEC_TABLEEMB")` in the AttributeOverrides. I believe this is a correct schema generation.

— XML Mapping Usecase —

orm.xml:

Hibernate generated schema:

Notice now how the primary table `XMLEmbedMultiTableEnt` DOES contain the Embeddable values; as well as the Secondary Table.

The issue however, may be more complex than JUST the schema generation being wrong. If I use the Annotation Usecase schema (which is correct), but run with the XML mapping configuration, Hibernate assumes that the primary table `XMLEmbedMultiTableEnt` should have the columns that should be attributeoverridden to the Secondary Table and fails with an exception on transaction commit:

Activity

Show:

Will Dazey December 10, 2021 at 4:17 AM

Yes, my conclusion is that the JPA specification is not clear on specifying any default access type for XML configuration. Hibernate defaults to AccessType.PROPERTY for XML configuration and for my test above, the super class getter/setting methods with this unexpected default accesstype was causing unexpected behavior.

I believe this case can be closed. Thanks you for the assistance!

Christian Beikov November 15, 2021 at 12:12 PM

Sounds to me like this is not clearly specified and thus it is the user responsibility to set the access type. AFAIU this is not a bug then?

Will Dazey November 14, 2021 at 9:59 PM

After looking at this case further, I discovered that if I explicitly set 'access="FIELD"' on the orm.xml mapping, then the DDL starts generating as expected.

It appears that XML processing is defaulting to use AccessType.PROPERTY for Hibernate (this appears to be at odds with other persistent providers like EclipseLink and OpenJPA who default to FIELD). This is causing the problem as inherited methods are being treated as persistent fields, even if there are no persistent fields.

 

The spec defines the default type for annotations quite well in section 2.3.1, but then only mention XML configuration with a footnote:

The use of XML as an alternative and the interaction between Java language annotations and XML elements in defining default
and explicit access types is described in Chapter 12.

Unfortunately, Chapter 12 fails to ever describe a default for XML elements other than simply how to override the default with the “access” XML configuration.

 

I think perhaps the only comment of substance I can find is the following from section 2.3.1:

It is an error if a default access type cannot be determined and an access type is not explicitly specified
by means of annotations or the XML descriptor.

For XML, it may be argued that a default access type cannot be determined given no annotations or explicit instructions. And if no explicit access type is specified, then providers should error.

Thoughts?

Will Dazey November 14, 2021 at 8:47 PM
Edited

Ok, so it turned out that the reason for the WARNINGS was because I did not have getter/setter methods specified on the XML entity classes. It took me a bit to think of that as a possibility as the messages didn’t really imply that to be the issue, but after adding those methods, it started working.

This may to be another bug as I am not required to set getter/setter methods on the annotation entity classes. Or maybe a place for messaging improvement… But section 2.3.1 of the specification is pretty vague on the default access type for XML based configuration… it may just be a grey area and provider specific behavior.

Will Dazey November 14, 2021 at 7:15 PM
Edited

No identifier specified for entity: org.hibernate.bugs.hhh14910.model.xml.XMLEmbedMultiTableEnt

Looking at the trace, I am seeing the following:

Can you see anything wrong with my configuration? These warnings seem to be inconsistent with my model. I can’t see anything obviously wrong, but I’m probably just missing something.

Ex. org.hibernate.bugs.hhh14910.model.xml.XMLEmbedMultiTableEnt.name not found in class

That field actually does exist in the class. The class is listed in the persistence.xml. Removing the <exclude-unlisted-classes> doesn't seem to change this

Rejected

Details

Assignee

Reporter

Components

Affects versions

Priority

Created November 4, 2021 at 4:07 PM
Updated December 10, 2021 at 9:43 AM
Resolved December 10, 2021 at 9:43 AM