[hibernate-dev] Composite IDs with a null property/field
Emmanuel Bernard
emmanuel at hibernate.org
Wed Dec 11 04:12:16 EST 2019
Just talking about simple id, even if we allow the column to be nullable
(if the DB even allows that), I don't think Hibernate allows null to be
a valid id value. Because null means I don't know or not applicable.
I think in the past we argued the same for attributes of a composite id,
like you said, if one of the element can be nul, why is it in the id
property in the first place.
As for whether there is a strong implementation detail reason to not
allow it, I don't know but I assume the null checking assuming "not an
id" is pretty much all over the place.
Emmanuel
On 11 Dec 2019, at 3:37, Gail Badner wrote:
> 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
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev
More information about the hibernate-dev
mailing list