[hibernate-dev] Composite IDs with a null property/field

Gail Badner gbadner at redhat.com
Tue Dec 10 21:37:02 EST 2019


Currently, there is no way to load an entity that exists in the database
with a composite ID, if one of the composite ID columns is null.

This behavior is due to this code in ComponentType#hydrate:
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/type/ComponentType.java#L671-L675

Basically, if any field/property in a composite ID is null, Hibernate
assumes the entire ID is null. An entity cannot have a null ID, so it
returns null for the entity result.

I believe that Hibernate does allow a primary key column to be nullable.

TBH, it seems strange to have a property in a composite ID that can be
null. If it can be null, it seems that the property could be removed from
the composite key.

I don't see anything in the spec about a requirement that all composite ID
fields/properties must be non-null. Am I missing something?

The code I referenced above is 13 years old. Does anyone have insight into
why Hibernate does this?

Thanks,
Gail


More information about the hibernate-dev mailing list