use of org.hibernate.type.descriptor.java.JavaType in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitLiteral.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// General expressions
@Override
public Expression visitLiteral(SqmLiteral<?> literal) {
if (literal instanceof SqmLiteralNull) {
MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
if (mappingModelExpressible == null) {
mappingModelExpressible = determineCurrentExpressible(literal);
}
if (mappingModelExpressible instanceof BasicValuedMapping) {
return new QueryLiteral<>(null, (BasicValuedMapping) mappingModelExpressible);
}
final MappingModelExpressible<?> keyExpressible = getKeyExpressible(mappingModelExpressible);
if (keyExpressible == null) {
// Default to the Object type
return new QueryLiteral<>(null, basicType(Object.class));
}
final List<Expression> expressions = new ArrayList<>(keyExpressible.getJdbcTypeCount());
keyExpressible.forEachJdbcType((index, jdbcMapping) -> expressions.add(new QueryLiteral<>(null, (BasicValuedMapping) jdbcMapping)));
return new SqlTuple(expressions, mappingModelExpressible);
}
final MappingModelExpressible<?> inferableExpressible = resolveInferredType();
if (inferableExpressible instanceof ConvertibleModelPart) {
final ConvertibleModelPart convertibleModelPart = (ConvertibleModelPart) inferableExpressible;
if (convertibleModelPart.getValueConverter() != null) {
return new QueryLiteral<>(literal.getLiteralValue(), convertibleModelPart);
}
} else // Special case for when we create an entity literal through the JPA CriteriaBuilder.literal API
if (inferableExpressible instanceof EntityDiscriminatorMapping) {
final EntityDiscriminatorMapping discriminatorMapping = (EntityDiscriminatorMapping) inferableExpressible;
final Object literalValue = literal.getLiteralValue();
final EntityPersister mappingDescriptor;
if (literalValue instanceof Class<?>) {
mappingDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor((Class<?>) literalValue);
} else {
final JavaType<?> javaType = discriminatorMapping.getJdbcMapping().getJavaTypeDescriptor();
final Object discriminatorValue;
if (javaType.getJavaTypeClass().isInstance(literalValue)) {
discriminatorValue = literalValue;
} else if (literalValue instanceof CharSequence) {
discriminatorValue = javaType.fromString((CharSequence) literalValue);
} else if (creationContext.getSessionFactory().getJpaMetamodel().getJpaCompliance().isLoadByIdComplianceEnabled()) {
discriminatorValue = literalValue;
} else {
discriminatorValue = javaType.coerce(literalValue, null);
}
final String entityName = discriminatorMapping.getConcreteEntityNameForDiscriminatorValue(discriminatorValue);
mappingDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
}
return new EntityTypeLiteral(mappingDescriptor);
}
final MappingModelExpressible<?> expressible;
final MappingModelExpressible<?> localExpressible = SqmMappingModelHelper.resolveMappingModelExpressible(literal, creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel(), getFromClauseAccess()::findTableGroup);
if (localExpressible == null) {
expressible = getElementExpressible(inferableExpressible);
} else {
final MappingModelExpressible<?> elementExpressible = getElementExpressible(localExpressible);
if (elementExpressible instanceof BasicType) {
expressible = InferredBasicValueResolver.resolveSqlTypeIndicators(this, (BasicType) elementExpressible, literal.getJavaTypeDescriptor());
} else {
expressible = elementExpressible;
}
}
if (expressible instanceof EntityIdentifierMapping && literal.getNodeType() instanceof EntityTypeImpl) {
return new QueryLiteral<>(((EntityIdentifierMapping) expressible).getIdentifier(literal.getLiteralValue()), (BasicValuedMapping) expressible);
}
if (expressible instanceof BasicValuedMapping) {
return new QueryLiteral<>(literal.getLiteralValue(), (BasicValuedMapping) expressible);
}
// Handling other values might seem unnecessary, but with JPA Criteria it is totally possible to have such literals
if (expressible instanceof EmbeddableValuedModelPart) {
final EmbeddableValuedModelPart embeddableValuedModelPart = (EmbeddableValuedModelPart) expressible;
final List<Expression> list = new ArrayList<>(embeddableValuedModelPart.getJdbcTypeCount());
embeddableValuedModelPart.forEachJdbcValue(literal.getLiteralValue(), null, (selectionIndex, value, jdbcMapping) -> list.add(new QueryLiteral<>(value, (BasicValuedMapping) jdbcMapping)), null);
return new SqlTuple(list, expressible);
} else if (expressible instanceof EntityValuedModelPart) {
final EntityValuedModelPart entityValuedModelPart = (EntityValuedModelPart) expressible;
final Object associationKey;
final ModelPart associationKeyPart;
if (entityValuedModelPart instanceof Association) {
final Association association = (Association) entityValuedModelPart;
final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor();
associationKey = foreignKeyDescriptor.getAssociationKeyFromSide(literal.getLiteralValue(), association.getSideNature().inverse(), null);
associationKeyPart = foreignKeyDescriptor.getPart(association.getSideNature());
} else {
final EntityIdentifierMapping identifierMapping = entityValuedModelPart.getEntityMappingType().getIdentifierMapping();
associationKeyPart = identifierMapping;
associationKey = identifierMapping.getIdentifier(literal.getLiteralValue(), null);
}
if (associationKeyPart instanceof BasicValuedMapping) {
return new QueryLiteral<>(associationKey, (BasicValuedMapping) associationKeyPart);
} else {
final List<Expression> list = new ArrayList<>(associationKeyPart.getJdbcTypeCount());
associationKeyPart.forEachJdbcValue(associationKey, null, (selectionIndex, value, jdbcMapping) -> list.add(new QueryLiteral<>(value, (BasicValuedMapping) jdbcMapping)), null);
return new SqlTuple(list, associationKeyPart);
}
} else {
return new QueryLiteral<>(literal.getLiteralValue(), creationContext.getSessionFactory().getTypeConfiguration().getBasicTypeRegistry().getRegisteredType(((BasicSqmPathSource<?>) literal.getNodeType()).getSqmPathType().getJavaType().getName()));
}
}
use of org.hibernate.type.descriptor.java.JavaType in project hibernate-orm by hibernate.
the class AbstractEntityPersister method generateNonIdAttributeMapping.
private AttributeMapping generateNonIdAttributeMapping(NonIdentifierAttribute tupleAttrDefinition, Property bootProperty, int stateArrayPosition, MappingModelCreationProcess creationProcess) {
final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final String attrName = tupleAttrDefinition.getName();
final Type attrType = tupleAttrDefinition.getType();
final int propertyIndex = getPropertyIndex(bootProperty.getName());
final String tableExpression = getTableName(getPropertyTableNumbers()[propertyIndex]);
final String[] attrColumnNames = getPropertyColumnNames(propertyIndex);
final PropertyAccess propertyAccess = getRepresentationStrategy().resolvePropertyAccess(bootProperty);
if (propertyIndex == getVersionProperty()) {
Column column = bootProperty.getValue().getColumns().get(0);
return MappingModelCreationHelper.buildBasicAttributeMapping(attrName, getNavigableRole().append(bootProperty.getName()), stateArrayPosition, bootProperty, this, (BasicType<?>) attrType, tableExpression, attrColumnNames[0], false, null, null, column.getSqlType(), column.getLength(), column.getPrecision(), column.getScale(), propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
}
if (attrType instanceof BasicType) {
final Value bootValue = bootProperty.getValue();
final String attrColumnExpression;
final boolean isAttrColumnExpressionFormula;
final String customReadExpr;
final String customWriteExpr;
final String columnDefinition;
final Long length;
final Integer precision;
final Integer scale;
if (bootValue instanceof DependantValue) {
attrColumnExpression = attrColumnNames[0];
isAttrColumnExpressionFormula = false;
customReadExpr = null;
customWriteExpr = null;
Column column = bootValue.getColumns().get(0);
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
} else {
final BasicValue basicBootValue = (BasicValue) bootValue;
if (attrColumnNames[0] != null) {
attrColumnExpression = attrColumnNames[0];
isAttrColumnExpressionFormula = false;
final List<Selectable> selectables = basicBootValue.getSelectables();
assert !selectables.isEmpty();
final Selectable selectable = selectables.get(0);
assert attrColumnExpression.equals(selectable.getText(sessionFactory.getJdbcServices().getDialect()));
customReadExpr = selectable.getTemplate(dialect, sessionFactory.getQueryEngine().getSqmFunctionRegistry());
customWriteExpr = selectable.getCustomWriteExpression();
Column column = bootValue.getColumns().get(0);
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
} else {
final String[] attrColumnFormulaTemplate = propertyColumnFormulaTemplates[propertyIndex];
attrColumnExpression = attrColumnFormulaTemplate[0];
isAttrColumnExpressionFormula = true;
customReadExpr = null;
customWriteExpr = null;
columnDefinition = null;
length = null;
precision = null;
scale = null;
}
}
return MappingModelCreationHelper.buildBasicAttributeMapping(attrName, getNavigableRole().append(bootProperty.getName()), stateArrayPosition, bootProperty, this, (BasicType<?>) attrType, tableExpression, attrColumnExpression, isAttrColumnExpressionFormula, customReadExpr, customWriteExpr, columnDefinition, length, precision, scale, propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
} else if (attrType instanceof AnyType) {
final JavaType<Object> baseAssociationJtd = sessionFactory.getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Object.class);
final AnyType anyType = (AnyType) attrType;
return new DiscriminatedAssociationAttributeMapping(navigableRole.append(bootProperty.getName()), baseAssociationJtd, this, stateArrayPosition, entityMappingType -> new StateArrayContributorMetadata() {
private final MutabilityPlan<?> mutabilityPlan = new DiscriminatedAssociationAttributeMapping.MutabilityPlanImpl(anyType);
private final boolean nullable = bootProperty.isOptional();
private final boolean insertable = bootProperty.isInsertable();
private final boolean updateable = bootProperty.isUpdateable();
private final boolean optimisticallyLocked = bootProperty.isOptimisticLocked();
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan<?> getMutabilityPlan() {
return mutabilityPlan;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public boolean isInsertable() {
return insertable;
}
@Override
public boolean isUpdatable() {
return updateable;
}
@Override
public boolean isIncludedInDirtyChecking() {
return updateable;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return optimisticallyLocked;
}
}, bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE, propertyAccess, bootProperty, (AnyType) attrType, (Any) bootProperty.getValue(), creationProcess);
} else if (attrType instanceof CompositeType) {
return MappingModelCreationHelper.buildEmbeddedAttributeMapping(attrName, stateArrayPosition, bootProperty, this, (CompositeType) attrType, tableExpression, null, propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
} else if (attrType instanceof CollectionType) {
return MappingModelCreationHelper.buildPluralAttributeMapping(attrName, stateArrayPosition, bootProperty, this, propertyAccess, tupleAttrDefinition.getCascadeStyle(), getFetchMode(stateArrayPosition), creationProcess);
} else if (attrType instanceof EntityType) {
return MappingModelCreationHelper.buildSingularAssociationAttributeMapping(attrName, getNavigableRole().append(attrName), stateArrayPosition, bootProperty, this, this, (EntityType) attrType, propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
}
return null;
}
use of org.hibernate.type.descriptor.java.JavaType in project hibernate-orm by hibernate.
the class ListResultsConsumer method consume.
@Override
public List<R> consume(JdbcValues jdbcValues, SharedSessionContractImplementor session, JdbcValuesSourceProcessingOptions processingOptions, JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState, RowProcessingStateStandardImpl rowProcessingState, RowReader<R> rowReader) {
final PersistenceContext persistenceContext = session.getPersistenceContext();
RuntimeException ex = null;
try {
persistenceContext.getLoadContexts().register(jdbcValuesSourceProcessingState);
final List<R> results = new ArrayList<>();
boolean uniqueRows = false;
if (uniqueSemantic != UniqueSemantic.NONE) {
final Class<R> resultJavaType = rowReader.getResultJavaType();
if (resultJavaType != null && !resultJavaType.isArray()) {
final EntityPersister entityDescriptor = session.getFactory().getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(resultJavaType);
if (entityDescriptor != null) {
uniqueRows = true;
}
}
}
if (uniqueRows) {
final List<JavaType> resultJavaTypes = rowReader.getResultJavaTypes();
assert resultJavaTypes.size() == 1;
final JavaType<R> resultJavaType = resultJavaTypes.get(0);
while (rowProcessingState.next()) {
final R row = rowReader.readRow(rowProcessingState, processingOptions);
boolean add = true;
for (R existingRow : results) {
if (resultJavaType.areEqual(existingRow, row)) {
if (uniqueSemantic == UniqueSemantic.ASSERT && !rowProcessingState.hasCollectionInitializers()) {
throw new HibernateException("More than one row with the given identifier was found: " + jdbcValuesSourceProcessingState.getExecutionContext().getEntityId() + ", for class: " + rowReader.getResultJavaType().getName());
}
add = false;
break;
}
}
if (add) {
results.add(row);
}
rowProcessingState.finishRowProcessing();
}
} else {
while (rowProcessingState.next()) {
results.add(rowReader.readRow(rowProcessingState, processingOptions));
rowProcessingState.finishRowProcessing();
}
}
try {
jdbcValuesSourceProcessingState.finishUp();
} finally {
persistenceContext.getLoadContexts().deregister(jdbcValuesSourceProcessingState);
}
// noinspection unchecked
final ResultListTransformer<R> resultListTransformer = (ResultListTransformer<R>) jdbcValuesSourceProcessingState.getExecutionContext().getQueryOptions().getResultListTransformer();
if (resultListTransformer != null) {
return resultListTransformer.transformList(results);
}
return results;
} catch (RuntimeException e) {
ex = e;
} finally {
try {
rowReader.finishUp(jdbcValuesSourceProcessingState);
jdbcValues.finishUp(session);
persistenceContext.initializeNonLazyCollections();
} catch (RuntimeException e) {
if (ex != null) {
ex.addSuppressed(e);
} else {
ex = e;
}
} finally {
if (ex != null) {
throw ex;
}
}
}
throw new IllegalStateException("Should not reach this!");
}
Aggregations