use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEmbeddableInitializer method applyMapsId.
private void applyMapsId(RowProcessingState processingState) {
final SharedSessionContractImplementor session = processingState.getSession();
if (embedded instanceof CompositeIdentifierMapping) {
final CompositeIdentifierMapping cid = (CompositeIdentifierMapping) embedded;
final EmbeddableMappingType mappedIdEmbeddable = cid.getMappedIdEmbeddableTypeDescriptor();
if (cid.hasContainingClass()) {
final EmbeddableMappingType virtualIdEmbeddable = embedded.getEmbeddableTypeDescriptor();
if (virtualIdEmbeddable == mappedIdEmbeddable) {
return;
}
virtualIdEmbeddable.forEachAttributeMapping((position, virtualIdAttribute) -> {
final AttributeMapping mappedIdAttribute = mappedIdEmbeddable.getAttributeMapping(position);
if (virtualIdAttribute instanceof ToOneAttributeMapping && !(mappedIdAttribute instanceof ToOneAttributeMapping)) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) virtualIdAttribute;
final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
final Object associationKey = fkDescriptor.getAssociationKeyFromSide(rowState[position], toOneAttributeMapping.getSideNature().inverse(), session);
rowState[position] = associationKey;
}
});
}
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class EmbeddedComponentType method isMethodOf.
@Override
public boolean isMethodOf(Method method) {
if (mappingModelPart() == null) {
throw new IllegalStateException("EmbeddableValuedModelPart not known yet");
}
final EmbeddableMappingType embeddable = mappingModelPart().getEmbeddableTypeDescriptor();
for (int i = 0; i < embeddable.getAttributeMappings().size(); i++) {
final AttributeMapping attributeMapping = embeddable.getAttributeMapping(i);
final Getter getter = attributeMapping.getPropertyAccess().getGetter();
final Method getterMethod = getter.getMethod();
if (getterMethod != null && getterMethod.equals(method)) {
return true;
}
}
return false;
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class InverseNonAggregatedIdentifierMapping method getIdentifier.
@Override
public Object getIdentifier(Object entity) {
if (hasContainingClass()) {
final Object id = identifierValueMapper.getRepresentationStrategy().getInstantiator().instantiate(null, // sessionFactory
null);
final List<AttributeMapping> attributeMappings = getEmbeddableTypeDescriptor().getAttributeMappings();
final List<AttributeMapping> idClassAttributeMappings = identifierValueMapper.getAttributeMappings();
final Object[] propertyValues = new Object[attributeMappings.size()];
for (int i = 0; i < propertyValues.length; i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
final Object o = attributeMapping.getPropertyAccess().getGetter().get(entity);
if (o == null) {
final AttributeMapping idClassAttributeMapping = idClassAttributeMappings.get(i);
if (idClassAttributeMapping.getPropertyAccess().getGetter().getReturnTypeClass().isPrimitive()) {
propertyValues[i] = idClassAttributeMapping.getExpressibleJavaType().getDefaultValue();
} else {
propertyValues[i] = null;
}
} else // JPA 2 @MapsId + @IdClass points to the pk of the entity
if (attributeMapping instanceof ToOneAttributeMapping && !(idClassAttributeMappings.get(i) instanceof ToOneAttributeMapping)) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
final ModelPart targetPart = toOneAttributeMapping.getForeignKeyDescriptor().getPart(toOneAttributeMapping.getSideNature().inverse());
if (targetPart instanceof EntityIdentifierMapping) {
propertyValues[i] = ((EntityIdentifierMapping) targetPart).getIdentifier(o);
} else {
propertyValues[i] = o;
assert false;
}
} else {
propertyValues[i] = o;
}
}
identifierValueMapper.setValues(id, propertyValues);
return id;
} else {
return entity;
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class NonAggregatedIdentifierMappingImpl method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
if (hasContainingClass()) {
final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
int offset = 0;
for (AttributeMapping attributeMapping : identifierValueMapper.getAttributeMappings()) {
offset += attributeMapping.forEachSelectable(offset, (columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add((ColumnReference) columnReference);
});
}
return new SqlTuple(columnReferences, this);
}
return super.toSqlExpression(tableGroup, clause, walker, sqlAstCreationState);
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class NonAggregatedIdentifierMappingImpl method setIdentifier.
@Override
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
final List<AttributeMapping> mappedIdAttributeMappings = identifierValueMapper.getAttributeMappings();
final Object[] propertyValues = new Object[mappedIdAttributeMappings.size()];
getEmbeddableTypeDescriptor().forEachAttributeMapping((position, attribute) -> {
final AttributeMapping mappedIdAttributeMapping = mappedIdAttributeMappings.get(position);
final Object o = mappedIdAttributeMapping.getPropertyAccess().getGetter().get(id);
if (attribute instanceof ToOneAttributeMapping && !(mappedIdAttributeMapping instanceof ToOneAttributeMapping)) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute;
final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType().getEntityPersister();
final EntityKey entityKey = session.generateEntityKey(o, entityPersister);
final PersistenceContext persistenceContext = session.getPersistenceContext();
// it is conceivable there is a proxy, so check that first
propertyValues[position] = persistenceContext.getProxy(entityKey);
if (propertyValues[position] == null) {
// otherwise look for an initialized version
propertyValues[position] = persistenceContext.getEntity(entityKey);
if (propertyValues[position] == null) {
// get the association out of the entity itself
propertyValues[position] = entityDescriptor.getPropertyValue(entity, toOneAttributeMapping.getAttributeName());
}
}
} else {
propertyValues[position] = o;
}
});
getEmbeddableTypeDescriptor().setValues(entity, propertyValues);
}
Aggregations