hibernate/hibernate-orm · error · IllegalArgumentException
Couldn't determine basic type for java type: {}
Error message
Couldn't determine basic type for java type: {} What it means
SqmExpression.cast(Class) resolves the target Java type through the session's TypeConfiguration via getBasicTypeForJavaType(). Only classes with a registered BasicType can serve as cast targets; for anything else (entity classes, embeddables, interfaces, arbitrary POJOs, unregistered custom types) Hibernate cannot build a cast expression and throws IllegalArgumentException naming the class.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/expression/SqmExpression.java:165
// safe cast, because we just checked
@SuppressWarnings("unchecked")
final SqmExpression<X> castExpression = (SqmExpression<X>) this;
return castExpression;
}
else {
final QueryEngine queryEngine = nodeBuilder().getQueryEngine();
final SqmCastTarget<?> target = new SqmCastTarget<>( (ReturnableType<?>) type, nodeBuilder() );
return queryEngine.getSqmFunctionRegistry().getFunctionDescriptor( "cast" )
.generateSqmExpression( asList( this, target ), (ReturnableType<X>) type, queryEngine );
}
}
@Nonnull
@Override
default <X> SqmExpression<X> cast(@Nonnull Class<X> type) {
final BasicType<X> basicType = nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType( type );
if ( basicType == null ) {
throw new IllegalArgumentException( "Couldn't determine basic type for java type: " + type.getName() );
}
return castAs( basicType );
}
@Nonnull
@Override
JpaPredicate notEqualTo(@Nonnull Expression<?> value);
@Nonnull
@Override
JpaPredicate notEqualTo(Object value);
}
View on GitHub (pinned to fad1729dce)
Solutions
- Cast to a standard basic type instead (String, Integer, Long, Double, BigDecimal, Boolean, dates).
- Resolve the BasicType explicitly and use castAs(): BasicType<X> t = nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType(X.class); if non-null, call expr.castAs(t).
- Register a JavaType/BasicType for the class globally with @JavaTypeRegistration (or map it with an AttributeConverter) so the lookup succeeds.
- For entity types use treatAs()/type() constructs rather than cast().
Example fix
// before
SqmExpression<Money> e = expr.cast( Money.class ); // no BasicType for Money -> IllegalArgumentException
// after - resolve the registered basic type and cast with it
BasicType<Money> moneyType = nodeBuilder().getTypeConfiguration()
.getBasicTypeForJavaType( Money.class );
SqmExpression<Money> e = expr.castAs( moneyType ); // register the type first if moneyType == null Defensive patterns
Strategy: validation
Validate before calling
BasicType<X> basicType = nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType( type );
if ( basicType == null ) {
throw new IllegalArgumentException( "No basic type registered for " + type.getName()
+ " - cast to a standard type or register a @JavaTypeRegistration" );
}
return expr.castAs( basicType ); Type guard
static boolean isCastableToBasicType(NodeBuilder nb, Class<?> javaType) {
return nb.getTypeConfiguration().getBasicTypeForJavaType( javaType ) != null;
} Prevention
- Whitelist cast targets to known basic types (String, numeric wrappers, temporal types).
- Register custom value classes with @JavaTypeRegistration or an AttributeConverter before casting to them.
- Use castAs(BasicType) after an explicit lookup instead of cast(Class) when the target is dynamic.
When it happens
Trigger: Calling expression.cast(X.class) where X has no registered basic type: casting to an entity or embeddable class, to Object or Serializable, to an array type, to a custom value class without @JavaTypeRegistration/@Converter mapping, or to a java.time/jdbc type the current dialect does not register.
Common situations: Cast targets chosen dynamically at runtime (the Class comes from a variable or user input); attempts to widen an expression by casting to Object; custom types registered only for specific dialects or lost during refactors; Hibernate upgrades where a formerly registered default type disappeared.
Related errors
- Enum literal cannot be cast to BigDecimal
- Enum literal cannot be cast to BigInteger
- MappedSuperclassType cannot be used to create an SqmPath - t
- LHS cannot be null for a sub-navigable reference - {}
- Not correlated
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/5c51711e293134b7.
Report an issue: GitHub.