use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractCompositeIdentifierMapping method forEachJdbcValue.
@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
int span = 0;
final List<AttributeMapping> attributeMappings = getEmbeddableTypeDescriptor().getAttributeMappings();
for (int i = 0; i < attributeMappings.size(); i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
if (attributeMapping instanceof ToOneAttributeMapping) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
final Object identifier = fkDescriptor.getAssociationKeyFromSide(o, toOneAttributeMapping.getSideNature().inverse(), session);
span += fkDescriptor.forEachJdbcValue(identifier, clause, span + offset, valuesConsumer, session);
} else {
span += attributeMapping.forEachJdbcValue(o, clause, span + offset, valuesConsumer, session);
}
}
return span;
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method checkIdClass.
private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadType loadType, final Class<?> idClass) {
// we may have the jpa requirement of allowing find-by-id where id is the "simple pk value" of a
// dependent objects parent. This is part of its generally goofy derived identity "feature"
final EntityIdentifierMapping idMapping = persister.getIdentifierMapping();
if (idMapping instanceof CompositeIdentifierMapping) {
final CompositeIdentifierMapping compositeIdMapping = (CompositeIdentifierMapping) idMapping;
final List<AttributeMapping> attributeMappings = compositeIdMapping.getPartMappingType().getAttributeMappings();
if (attributeMappings.size() == 1) {
final AttributeMapping singleIdAttribute = attributeMappings.get(0);
if (singleIdAttribute.getMappedType() instanceof EntityMappingType) {
final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
final MappingType parentIdType = parentIdTargetIdMapping instanceof CompositeIdentifierMapping ? ((CompositeIdentifierMapping) parentIdTargetIdMapping).getMappedIdEmbeddableTypeDescriptor() : parentIdTargetIdMapping.getMappedType();
if (parentIdType.getMappedJavaType().getJavaTypeClass().isInstance(event.getEntityId())) {
// yep that's what we have...
loadByDerivedIdentitySimplePkValue(event, loadType, persister, compositeIdMapping, (EntityPersister) parentIdTargetMapping);
return;
}
}
} else if (idMapping instanceof NonAggregatedIdentifierMapping) {
if (idClass.isInstance(event.getEntityId())) {
return;
}
}
}
throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method visitNullnessPredicate.
@Override
public void visitNullnessPredicate(NullnessPredicate nullnessPredicate) {
final Expression expression = nullnessPredicate.getExpression();
final String predicateValue;
if (nullnessPredicate.isNegated()) {
predicateValue = " is not null";
} else {
predicateValue = " is null";
}
final SqlTuple tuple;
if ((tuple = SqlTupleContainer.getSqlTuple(expression)) != null) {
String separator = NO_SEPARATOR;
// as the embeddable is not considered as null, if at least one sub-part is not null
if (nullnessPredicate.isNegated() && expression.getExpressionType() instanceof AttributeMapping) {
appendSql('(');
for (Expression exp : tuple.getExpressions()) {
appendSql(separator);
exp.accept(this);
appendSql(predicateValue);
separator = " or ";
}
appendSql(')');
} else // For the is null check, and also for tuples in SQL in general,
// the semantics is that all sub-parts must match the predicate
{
for (Expression exp : tuple.getExpressions()) {
appendSql(separator);
exp.accept(this);
appendSql(predicateValue);
separator = " and ";
}
}
} else {
expression.accept(this);
appendSql(predicateValue);
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method getPropertyValue.
@Override
public Object getPropertyValue(Object object, String propertyName) {
final int dotIndex = propertyName.indexOf('.');
final String basePropertyName = dotIndex == -1 ? propertyName : propertyName.substring(0, dotIndex);
final AttributeMapping attributeMapping = findAttributeMapping(basePropertyName);
ManagedMappingType baseValueType = null;
Object baseValue = null;
if (attributeMapping != null) {
baseValue = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
if (dotIndex != -1) {
baseValueType = (ManagedMappingType) attributeMapping.getMappedType();
}
} else if (identifierMapping instanceof NonAggregatedIdentifierMapping) {
final EmbeddedAttributeMapping embeddedAttributeMapping = (EmbeddedAttributeMapping) findAttributeMapping(NavigableRole.IDENTIFIER_MAPPER_PROPERTY);
final AttributeMapping mapping = embeddedAttributeMapping.getMappedType().findAttributeMapping(basePropertyName);
if (mapping != null) {
baseValue = mapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
if (dotIndex != -1) {
baseValueType = (ManagedMappingType) mapping.getMappedType();
}
}
}
return getPropertyValue(baseValue, baseValueType, propertyName, dotIndex);
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method resolveDirtyAttributeIndexes.
@Override
public int[] resolveDirtyAttributeIndexes(final Object[] currentState, final Object[] previousState, final String[] attributeNames, final SessionImplementor session) {
final BitSet mutablePropertiesIndexes = entityMetamodel.getMutablePropertiesIndexes();
final int estimatedSize = attributeNames == null ? 0 : attributeNames.length + mutablePropertiesIndexes.cardinality();
final List<Integer> fields = new ArrayList<>(estimatedSize);
if (estimatedSize == 0) {
return ArrayHelper.EMPTY_INT_ARRAY;
}
if (!mutablePropertiesIndexes.isEmpty()) {
// We have to check the state for "mutable" properties as dirty tracking isn't aware of mutable types
final Type[] propertyTypes = entityMetamodel.getPropertyTypes();
final boolean[] propertyCheckability = entityMetamodel.getPropertyCheckability();
mutablePropertiesIndexes.stream().forEach(i -> {
// This is kindly borrowed from org.hibernate.type.TypeHelper.findDirty
final boolean dirty = currentState[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY && // Consider mutable properties as dirty if we don't have a previous state
(previousState == null || previousState[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY || (propertyCheckability[i] && propertyTypes[i].isDirty(previousState[i], currentState[i], propertyColumnUpdateable[i], session)));
if (dirty) {
fields.add(i);
}
});
}
if (attributeNames.length != 0) {
final boolean[] propertyUpdateability = entityMetamodel.getPropertyUpdateability();
// Sort attribute names so that we can traverse mappings efficiently
Arrays.sort(attributeNames);
int index = 0;
for (final AttributeMapping attributeMapping : attributeMappings) {
final String attributeName = attributeMapping.getAttributeName();
final int nameLength = attributeName.length();
final String currentAttributeName = attributeNames[index];
int position = ((StateArrayContributorMapping) attributeMapping).getStateArrayPosition();
if (currentAttributeName.startsWith(attributeName) && ((currentAttributeName.length() == nameLength || currentAttributeName.charAt(nameLength) == '.'))) {
if (propertyUpdateability[position] && !fields.contains(position)) {
fields.add(position);
}
index++;
if (index < attributeNames.length) {
// Skip duplicates
do {
if (attributeNames[index].equals(attributeName)) {
index++;
} else {
break;
}
} while (index < attributeNames.length);
} else {
break;
}
}
}
}
return ArrayHelper.toIntArray(fields);
}
Aggregations