Skip to content

Commit

Permalink
HHH-18564 Handle attribute converters for query literals
Browse files Browse the repository at this point in the history
Allow both domain and relational forms when an attribute converter is present
  • Loading branch information
mbladel committed Oct 31, 2024
1 parent 9ff88bb commit 4c17935
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5803,9 +5803,13 @@ protected void renderLiteral(Literal literal, boolean castParameter) {
}
}
else {
final Object literalValue = literal.getLiteralValue();
final BasicValueConverter<Object, ?> valueConverter = jdbcMapping.getValueConverter();
literalFormatter.appendJdbcLiteral(
this,
literal.getLiteralValue(),
valueConverter == null || jdbcMapping.getJdbcJavaType().isInstance( literalValue ) ?
literalValue :
valueConverter.toRelationalValue( literalValue ),
dialect,
getWrapperOptions()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.basic.BasicResult;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;

/**
* Represents a literal in the SQL AST. This form accepts a {@link BasicValuedMapping}
Expand All @@ -32,11 +33,25 @@ public class QueryLiteral<T> implements Literal, DomainResultProducer<T> {
private final SqlExpressible expressible;

public QueryLiteral(T value, SqlExpressible expressible) {
assert value == null || expressible.getJdbcMapping().getJdbcJavaType().isInstance( value );
assert valueInstanceOfExpressible( value, expressible );
this.value = value;
this.expressible = expressible;
}

private static <T> boolean valueInstanceOfExpressible(T value, SqlExpressible expressible) {
if ( value == null ) {
return true;
}

final JdbcMapping jdbcMapping = expressible.getJdbcMapping();
if ( jdbcMapping.getJdbcJavaType().isInstance( value )) {
return true;
}

final BasicValueConverter<?, ?> valueConverter = jdbcMapping.getValueConverter();
return valueConverter != null && valueConverter.getDomainJavaType().isInstance( value );
}

@Override
public T getLiteralValue() {
return value;
Expand Down

0 comments on commit 4c17935

Please sign in to comment.