IllegalArgumentException when composite ID has IDENTITY generated value
Description
Attachments
causes
is duplicated by
relates to
Activity

Maksym.Tsybulskyi August 26, 2022 at 6:33 PM
We faced the same issue working with Vitess. We used a composite key to have a sharding key as a necessary part. Unfortunately, it’s impossible to use the SEQUENCE strategy with Vitess so this fix is essential for us. Is it any estimation of when the fix will be ready? I see that the task is “In Progress“ from the end of 2019.
Steve Ebersole December 9, 2021 at 3:30 PMEdited
For instance, the JPA spec does not even say if generated identifiers are allowed in a Composite Identifier.
Just for clarification, the spec actually explicitly says you cannot. None of @Id
, GeneratedValue
, etc may be specified on the @Embeddable
used as an @EmbeddedId
.
Now for “virtual ids” (with or without @IdClass
) this does in fact work (there may be limitations here wrt IDENTITY which we may or may not classify as bug). We only added support for this because older SpecJ tests mapped them that way (even though the spec does not say this is supported) and trying to argue with those people is an endeavor in futility.

IT Support November 12, 2021 at 12:54 PM
Curious, why the change from bug to enhancement?

IT Support April 22, 2020 at 9:58 PM
regarding the
there is a PR related to this issue ( ), you can try to verify if it solves the problem
I am finding the removing the column issue with SQLServer2012Dialect quite difficult. SQL Server requires an Identity column to be omitted from the insert, not just NULL / DEFAULT.

IT Support April 21, 2020 at 3:16 PM
I have found that this fails with sequences too, when the composite identifier is another entity.
This works:
but, this does not:
The exception thrown is:
Details
Details
Assignee
Reporter

When composite ID has at least one IDENTITY generated value, you cannot persist an entity.
Suppose you have a class (getters-setters and other stuff is cut to be shorter):
When you try to persist in debug
you will get to
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(...) line 117:
Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );
then org.hibernate.mapping.Component.ValueGenerationPlan.execute(...) line 440
will set
generatedValue = IdentifierGeneratorHelper.POST_INSERT_INDICATOR
later org.hibernate.property.BasicPropertyAccessor.BasicSetter.set(...) line 68 will try to set
IdentifierGeneratorHelper.POST_INSERT_INDICATOR
(String) to Long and this will cause an exception, which will bubble up, but not handled appropriately, so the code will never return to org.hibernate.event.internal.AbstractSaveEventListener line 124 for comparing generated ID withIdentifierGeneratorHelper.POST_INSERT_INDICATOR
This is not happening when ID is non-composite.
Also I have reported somewhat related bug #HHH-9657 . In general looks like Hibernate is a bit weak with composite IDENTITY IDs