use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class EmbeddableMappingTypeImpl method forEachJdbcValue.
@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer consumer, SharedSessionContractImplementor session) {
int span = 0;
for (int i = 0; i < attributeMappings.size(); i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
if (attributeMapping instanceof PluralAttributeMapping) {
continue;
}
final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
span += attributeMapping.forEachJdbcValue(o, clause, span + offset, consumer, session);
}
return span;
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class EmbeddableMappingTypeImpl method breakDownJdbcValues.
@Override
public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) {
if (domainValue instanceof Object[]) {
final Object[] values = (Object[]) domainValue;
assert values.length == attributeMappings.size();
for (int i = 0; i < attributeMappings.size(); i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
final Object attributeValue = values[i];
attributeMapping.breakDownJdbcValues(attributeValue, valueConsumer, session);
}
} else {
attributeMappings.forEach((attributeMapping) -> {
final Object attributeValue = attributeMapping.getPropertyAccess().getGetter().get(domainValue);
attributeMapping.breakDownJdbcValues(attributeValue, valueConsumer, session);
});
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class IdClassEmbeddable method getIdentifier.
@Override
public Object getIdentifier(Object entity, SharedSessionContractImplementor session) {
final Object id = representationStrategy.getInstantiator().instantiate(null, sessionFactory);
final List<AttributeMapping> virtualIdAttribute = virtualIdEmbeddable.getAttributeMappings();
final List<AttributeMapping> idClassAttribute = getAttributeMappings();
final Object[] propertyValues = new Object[virtualIdAttribute.size()];
for (int i = 0; i < propertyValues.length; i++) {
final AttributeMapping attributeMapping = virtualIdAttribute.get(i);
final Object o = attributeMapping.getPropertyAccess().getGetter().get(entity);
if (o == null) {
final AttributeMapping idClassAttributeMapping = idClassAttribute.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 && !(idClassAttribute.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;
}
}
setValues(id, propertyValues);
return id;
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class IdClassEmbeddable method disassemble.
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
final List<AttributeMapping> attributeMappings = getAttributeMappings();
// todo (6.0) : reduce to-one values to id here?
final Object[] result = new Object[attributeMappings.size()];
for (int i = 0; i < attributeMappings.size(); i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
result[i] = attributeMapping.disassemble(o, session);
}
return result;
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class IdClassEmbeddable method setIdentifier.
@Override
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
final SessionFactoryImplementor factory = session.getFactory();
final EntityPersister entityDescriptor = factory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entity.getClass());
final Object[] propertyValues = new Object[attributeMappings.size()];
virtualIdEmbeddable.forEachAttribute((position, virtualIdAttribute) -> {
final AttributeMapping idClassAttribute = attributeMappings.get(position);
final Object o = idClassAttribute.getPropertyAccess().getGetter().get(id);
if (virtualIdAttribute instanceof ToOneAttributeMapping && !(idClassAttribute instanceof ToOneAttributeMapping)) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) virtualIdAttribute;
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;
}
});
virtualIdEmbeddable.setValues(entity, propertyValues);
}
Aggregations