hibernate/hibernate-orm · error · UnsupportedOperationException
Unsupported identifier type:
Error message
Unsupported identifier type:
What it means
Inside a JSON aggregate Hibernate serializes referenced entities as just their identifier (serializeEntity -> serializeEntityIdentifier). It can write identifiers whose EntityIdentifierMapping is a SingleAttributeIdentifierMapping (simple @Id and single-attribute @EmbeddedId). The fallback branch only accepts an identifier VALUE that is itself a CompositeIdentifierMapping - which effectively never happens, since getIdentifier(value) returns the user's id object, not a mapping - so any other identifier shape throws UnsupportedOperationException naming the identifier's class.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/JsonGeneratingVisitor.java:304
protected void serializeEntityIdentifier(
Object value,
EntityIdentifierMapping identifierMapping,
WrapperOptions options,
JsonDocumentWriter writer) throws IOException {
final Object identifier = identifierMapping.getIdentifier( value );
if ( identifierMapping instanceof SingleAttributeIdentifierMapping singleAttribute ) {
writer.serializeJsonValue(
identifier,
singleAttribute.getJavaType(),
singleAttribute.getSingleJdbcMapping().getJdbcType(),
options
);
}
else if ( identifier instanceof CompositeIdentifierMapping composite ) {
visit( composite.getMappedType(), identifier, options, writer );
}
else {
throw new UnsupportedOperationException(
"Unsupported identifier type: " + identifier.getClass().getName() );
}
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Switch the referenced entity to @EmbeddedId (single-attribute composite id) or a simple @Id
- Do not reference the entity from the JSON aggregate - store basic FK value(s) (one column per id field) inside the embeddable instead
- Upgrade Hibernate ORM - identifier serialization for aggregates keeps being extended; report the case if it persists
Example fix
// before
class OrderUserId implements Serializable { Long tenantId; Long seq; } // @IdClass
@Entity @IdClass(OrderUserId.class)
class OrderUser { @Id Long tenantId; @Id Long seq; }
// after
@Entity
class OrderUser {
@EmbeddedId
private OrderUserId id; // single-attribute composite id serializes inside JSON aggregates
} Defensive patterns
Strategy: validation
Validate before calling
// Verify every entity referenced from JSON aggregates has a single-attribute id
for (EntityBinding b : referencedEntities) {
if (b.idUsesIdClass()) { // detected via @IdClass on the entity class
throw new IllegalStateException("Entity " + b.name + " with @IdClass cannot be referenced from a JSON aggregate");
}
} Prevention
- Prefer @EmbeddedId or simple @Id over @IdClass when entities may be referenced from aggregates
- Store basic FK values inside aggregates instead of entity references
- Re-test aggregate serialization after changing identifier strategies
When it happens
Trigger: A to-one reference (or element mapping) inside a JSON aggregate pointing at an entity whose primary key uses @IdClass (non-aggregated composite id): identifierMapping is not single-attribute and getIdentifier(value) returns the IdClass instance, so the else branch fires. Custom EntityIdentifierMapping implementations hit it too.
Common situations: Legacy schemas with @IdClass entities pulled into newer JSON aggregate mappings; embedding references to composite-key entities inside jsonb/OSON columns; migrating models to JSON storage while keeping @IdClass.
Related errors
- Could not find selectable [%s] in embeddable type [%s] for J
- Can't parse JSON object for selectable [%s] which is not of
- Expected JSON object end, but none found.
- Malformed JSON. Expected object but got: " + event
- Failed to serialize JSON mapping
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/2e7540eb5c903724.
Report an issue: GitHub.