use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class YearMappingTests method basicAssertions.
@Test
public void basicAssertions(SessionFactoryScope scope) {
final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
final EntityPersister entityDescriptor = sessionFactory.getMappingMetamodel().getEntityDescriptor(YearMappingTestEntity.class);
{
final BasicAttributeMapping yearAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("year");
assertThat(yearAttribute.getJdbcMapping().getJdbcType().getJdbcTypeCode()).isEqualTo(Types.INTEGER);
assertThat(yearAttribute.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass()).isEqualTo(Year.class);
}
{
final PluralAttributeMapping yearsAttribute = (PluralAttributeMapping) entityDescriptor.findAttributeMapping("years");
final BasicValuedCollectionPart elementDescriptor = (BasicValuedCollectionPart) yearsAttribute.getElementDescriptor();
assertThat(elementDescriptor.getJdbcMapping().getJdbcType().getJdbcTypeCode()).isEqualTo(Types.INTEGER);
assertThat(elementDescriptor.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass()).isEqualTo(Year.class);
}
{
final PluralAttributeMapping countByYearAttribute = (PluralAttributeMapping) entityDescriptor.findAttributeMapping("countByYear");
final BasicValuedCollectionPart keyDescriptor = (BasicValuedCollectionPart) countByYearAttribute.getIndexDescriptor();
assertThat(keyDescriptor.getJdbcMapping().getJdbcType().getJdbcTypeCode()).isEqualTo(Types.INTEGER);
assertThat(keyDescriptor.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass()).isEqualTo(Year.class);
}
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class TemporaryTable method createEntityTable.
public static TemporaryTable createEntityTable(EntityMappingType entityDescriptor, Function<String, String> temporaryTableNameAdjuster, Dialect dialect, RuntimeModelCreationContext runtimeModelCreationContext) {
return new TemporaryTable(entityDescriptor, temporaryTableNameAdjuster, dialect, temporaryTable -> {
final List<TemporaryTableColumn> columns = new ArrayList<>();
final PersistentClass entityBinding = runtimeModelCreationContext.getBootModel().getEntityBinding(entityDescriptor.getEntityName());
final IdentifierGenerator identifierGenerator = entityDescriptor.getEntityPersister().getIdentifierGenerator();
final boolean identityColumn = identifierGenerator instanceof PostInsertIdentifierGenerator;
final boolean hasOptimizer;
if (identityColumn) {
hasOptimizer = false;
final Iterator<JdbcMapping> jdbcMappings = entityDescriptor.getIdentifierMapping().getJdbcMappings().iterator();
for (Column column : entityBinding.getKey().getColumns()) {
final JdbcMapping jdbcMapping = jdbcMappings.next();
columns.add(new TemporaryTableColumn(temporaryTable, ENTITY_TABLE_IDENTITY_COLUMN, jdbcMapping, column.getSqlType(dialect, runtimeModelCreationContext.getMetadata()) + " " + dialect.getIdentityColumnSupport().getIdentityColumnString(column.getSqlTypeCode(runtimeModelCreationContext.getMetadata())), // column.isNullable()
true, true));
}
} else {
if (identifierGenerator instanceof OptimizableGenerator) {
final Optimizer optimizer = ((OptimizableGenerator) identifierGenerator).getOptimizer();
hasOptimizer = optimizer != null && optimizer.getIncrementSize() > 1;
} else {
hasOptimizer = false;
}
}
final Iterator<JdbcMapping> jdbcMappings = entityDescriptor.getIdentifierMapping().getJdbcMappings().iterator();
for (Column column : entityBinding.getKey().getColumns()) {
final JdbcMapping jdbcMapping = jdbcMappings.next();
columns.add(new TemporaryTableColumn(temporaryTable, column.getText(dialect), jdbcMapping, column.getSqlType(dialect, runtimeModelCreationContext.getMetadata()), // We have to set the identity column after the root table insert
column.isNullable() || identityColumn || hasOptimizer, !identityColumn && !hasOptimizer));
}
final EntityDiscriminatorMapping discriminatorMapping = entityDescriptor.getDiscriminatorMapping();
if (entityBinding.getDiscriminator() != null && !discriminatorMapping.isFormula()) {
final Column discriminator = entityBinding.getDiscriminator().getColumns().get(0);
columns.add(new TemporaryTableColumn(temporaryTable, discriminator.getText(dialect), discriminatorMapping.getJdbcMapping(), discriminator.getSqlType(dialect, runtimeModelCreationContext.getMetadata()), // We have to set the identity column after the root table insert
discriminator.isNullable()));
}
// Collect all columns for all entity subtype attributes
entityDescriptor.visitSubTypeAttributeMappings(attribute -> {
if (!(attribute instanceof PluralAttributeMapping)) {
final SimpleValue value = (SimpleValue) entityBinding.getSubclassProperty(attribute.getAttributeName()).getValue();
final Iterator<Selectable> columnIterator = value.getConstraintColumnIterator();
attribute.forEachSelectable((columnIndex, selection) -> {
final Selectable selectable = columnIterator.next();
if (selectable instanceof Column) {
final Column column = (Column) selectable;
columns.add(new TemporaryTableColumn(temporaryTable, selectable.getText(dialect), selection.getJdbcMapping(), column.getSqlType(dialect, runtimeModelCreationContext.getMetadata()), // Treat regular temporary table columns as nullable for simplicity
true));
}
});
}
});
if (hasOptimizer) {
// We add a special row number column that we can use to identify and join rows
final BasicType<Integer> integerBasicType = entityDescriptor.getEntityPersister().getFactory().getTypeConfiguration().getBasicTypeForJavaType(Integer.class);
final String rowNumberType;
if (dialect.supportsWindowFunctions()) {
rowNumberType = dialect.getTypeName(integerBasicType.getJdbcType().getJdbcTypeCode(), dialect.getSizeStrategy().resolveSize(integerBasicType.getJdbcType(), integerBasicType.getJavaTypeDescriptor(), null, null, null));
} else if (dialect.getIdentityColumnSupport().supportsIdentityColumns()) {
rowNumberType = dialect.getTypeName(integerBasicType.getJdbcType().getJdbcTypeCode(), dialect.getSizeStrategy().resolveSize(integerBasicType.getJdbcType(), integerBasicType.getJavaTypeDescriptor(), null, null, null)) + " " + dialect.getIdentityColumnSupport().getIdentityColumnString(integerBasicType.getJdbcType().getJdbcTypeCode());
} else {
LOG.multiTableInsertNotAvailable(entityBinding.getEntityName());
rowNumberType = dialect.getTypeName(integerBasicType.getJdbcType().getJdbcTypeCode(), dialect.getSizeStrategy().resolveSize(integerBasicType.getJdbcType(), integerBasicType.getJavaTypeDescriptor(), null, null, null));
}
columns.add(new TemporaryTableColumn(temporaryTable, "rn_", integerBasicType, rowNumberType, false, true));
}
return columns;
});
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class SqmUtil method createValueBindings.
private static void createValueBindings(JdbcParameterBindings jdbcParameterBindings, QueryParameterImplementor<?> domainParam, QueryParameterBinding<?> domainParamBinding, Bindable parameterType, List<JdbcParameter> jdbcParams, Object bindValue, Function<NavigablePath, TableGroup> tableGroupLocator, SharedSessionContractImplementor session) {
if (parameterType == null) {
throw new SqlTreeCreationException("Unable to interpret mapping-model type for Query parameter : " + domainParam);
}
if (parameterType instanceof EntityIdentifierMapping) {
final EntityIdentifierMapping identifierMapping = (EntityIdentifierMapping) parameterType;
final EntityMappingType entityMapping = identifierMapping.findContainingEntityMapping();
if (entityMapping.getRepresentationStrategy().getInstantiator().isInstance(bindValue, session.getFactory())) {
bindValue = identifierMapping.getIdentifier(bindValue);
}
} else if (parameterType instanceof EntityMappingType) {
final EntityIdentifierMapping identifierMapping = ((EntityMappingType) parameterType).getIdentifierMapping();
final EntityMappingType entityMapping = identifierMapping.findContainingEntityMapping();
parameterType = identifierMapping;
if (entityMapping.getRepresentationStrategy().getInstantiator().isInstance(bindValue, session.getFactory())) {
bindValue = identifierMapping.getIdentifier(bindValue);
}
} else if (parameterType instanceof EntityAssociationMapping) {
EntityAssociationMapping association = (EntityAssociationMapping) parameterType;
bindValue = association.getForeignKeyDescriptor().getAssociationKeyFromSide(bindValue, association.getSideNature().inverse(), session);
parameterType = association.getForeignKeyDescriptor();
} else if (parameterType instanceof PluralAttributeMapping) {
// for now, let's blow up and see where this happens and fix the specifics...
throw new NotYetImplementedFor6Exception("Binding parameters whose inferred type comes from plural attribute not yet implemented");
}
int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(bindValue, Clause.IRRELEVANT, parameterType, jdbcParams, session);
assert offset == jdbcParams.size();
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class InlineDeleteHandler method execute.
@Override
public int execute(DomainQueryExecutionContext executionContext) {
final List<Object> idsAndFks = MatchingIdSelectionHelper.selectMatchingIds(sqmDeleteStatement, domainParameterXref, executionContext);
if (idsAndFks == null || idsAndFks.isEmpty()) {
return 0;
}
final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
final String mutatingEntityName = sqmDeleteStatement.getTarget().getModel().getHibernateEntityName();
final EntityMappingType entityDescriptor = factory.getRuntimeMetamodels().getEntityMappingType(mutatingEntityName);
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(domainParameterXref.getQueryParameterCount());
// delete from the tables
final MutableInteger valueIndexCounter = new MutableInteger();
entityDescriptor.visitSubTypeAttributeMappings(attribute -> {
if (attribute instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttribute = (PluralAttributeMapping) attribute;
if (pluralAttribute.getSeparateCollectionTable() != null) {
// this collection has a separate collection table, meaning it is one of:
// 1) element-collection
// 2) many-to-many
// 3) one-to many using a dedicated join-table
//
// in all of these cases, we should clean up the matching rows in the
// collection table
final ModelPart fkTargetPart = pluralAttribute.getKeyDescriptor().getTargetPart();
final int valueIndex;
if (fkTargetPart instanceof EntityIdentifierMapping) {
valueIndex = 0;
} else {
if (valueIndexCounter.get() == 0) {
valueIndexCounter.set(entityDescriptor.getIdentifierMapping().getJdbcTypeCount());
}
valueIndex = valueIndexCounter.get();
valueIndexCounter.plus(fkTargetPart.getJdbcTypeCount());
}
executeDelete(pluralAttribute.getSeparateCollectionTable(), entityDescriptor, () -> fkTargetPart::forEachSelectable, idsAndFks, valueIndex, fkTargetPart, jdbcParameterBindings, executionContext);
}
}
});
entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnsVisitationSupplier) -> {
executeDelete(tableExpression, entityDescriptor, tableKeyColumnsVisitationSupplier, idsAndFks, 0, null, jdbcParameterBindings, executionContext);
});
return idsAndFks.size();
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class ElementCollectionSortingTest method checkSQLOrderBy.
private void checkSQLOrderBy(Session session, String entityName, String propertyName, SortOrder order) {
String roleName = entityName + "." + propertyName;
String alias = "alias1";
BasicCollectionPersister collectionPersister = (BasicCollectionPersister) session.getSessionFactory().getCollectionMetadata(roleName);
assertTrue(collectionPersister.hasOrdering());
PluralAttributeMapping attributeMapping = collectionPersister.getAttributeMapping();
assertThat(attributeMapping.getFetchableName(), is(propertyName));
OrderByFragmentImpl orderByFragment = (OrderByFragmentImpl) attributeMapping.getOrderByFragment();
List<OrderingSpecification> fragmentSpecs = orderByFragment.getFragmentSpecs();
assertThat(fragmentSpecs.size(), is(1));
assertThat(fragmentSpecs.get(0).getSortOrder(), is(order));
}
Aggregations