use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method generateNaturalIdMapping.
private NaturalIdMapping generateNaturalIdMapping(MappingModelCreationProcess creationProcess, PersistentClass bootEntityDescriptor) {
assert bootEntityDescriptor.hasNaturalId();
final int[] naturalIdAttributeIndexes = entityMetamodel.getNaturalIdentifierProperties();
assert naturalIdAttributeIndexes.length > 0;
if (naturalIdAttributeIndexes.length == 1) {
final String propertyName = entityMetamodel.getPropertyNames()[naturalIdAttributeIndexes[0]];
final AttributeMapping attributeMapping = findAttributeMapping(propertyName);
return new SimpleNaturalIdMapping((SingularAttributeMapping) attributeMapping, this, creationProcess);
}
// collect the names of the attributes making up the natural-id.
final Set<String> attributeNames = CollectionHelper.setOfSize(naturalIdAttributeIndexes.length);
for (int naturalIdAttributeIndex : naturalIdAttributeIndexes) {
attributeNames.add(this.getPropertyNames()[naturalIdAttributeIndex]);
}
// then iterate over the attribute mappings finding the ones having names
// in the collected names. iterate here because it is already alphabetical
final List<SingularAttributeMapping> collectedAttrMappings = new ArrayList<>();
this.attributeMappings.forEach((attributeMapping) -> {
if (attributeNames.contains(attributeMapping.getAttributeName())) {
collectedAttrMappings.add((SingularAttributeMapping) attributeMapping);
}
});
if (collectedAttrMappings.size() <= 1) {
throw new MappingException("Expected multiple natural-id attributes, but found only one: " + getEntityName());
}
return new CompoundNaturalIdMapping(this, collectedAttrMappings, creationProcess);
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method getPropertyValues.
@Override
public Object[] getPropertyValues(Object object) {
if (accessOptimizer != null) {
return accessOptimizer.getPropertyValues(object);
} else {
final BytecodeEnhancementMetadata enhancementMetadata = entityMetamodel.getBytecodeEnhancementMetadata();
final LazyAttributesMetadata lazyAttributesMetadata = enhancementMetadata.getLazyAttributesMetadata();
final Object[] values = new Object[getNumberOfAttributeMappings()];
for (int i = 0; i < attributeMappings.size(); i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
final AttributeMetadataAccess attributeMetadataAccess = attributeMapping.getAttributeMetadataAccess();
if (!lazyAttributesMetadata.isLazyAttribute(attributeMapping.getAttributeName()) || enhancementMetadata.isAttributeLoaded(object, attributeMapping.getAttributeName())) {
values[i] = attributeMetadataAccess.resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
} else {
values[i] = LazyPropertyInitializer.UNFETCHED_PROPERTY;
}
}
return values;
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method getPropertyValue.
private Object getPropertyValue(Object baseValue, ManagedMappingType baseValueType, String propertyName, int dotIndex) {
if (baseValueType == null) {
return baseValue;
}
final int nextDotIndex = propertyName.indexOf('.', dotIndex + 1);
final int endIndex = nextDotIndex == -1 ? propertyName.length() : nextDotIndex;
final AttributeMapping attributeMapping;
attributeMapping = baseValueType.findAttributeMapping(propertyName.substring(dotIndex + 1, endIndex));
baseValue = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(baseValue);
baseValueType = nextDotIndex == -1 ? null : (ManagedMappingType) attributeMapping.getMappedType();
return getPropertyValue(baseValue, baseValueType, propertyName, nextDotIndex);
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method setPropertyValue.
@Override
public void setPropertyValue(Object object, String propertyName, Object value) {
final AttributeMapping attributeMapping = (AttributeMapping) findSubPart(propertyName, this);
final AttributeMetadata attributeMetadata = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this);
attributeMetadata.getPropertyAccess().getSetter().set(object, value);
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AttributeOrderingTests method verifyRuntimeEntityMapping.
public void verifyRuntimeEntityMapping(EntityMappingType entityMappingType) {
final NaturalIdMapping naturalIdMapping = entityMappingType.getNaturalIdMapping();
assertThat(naturalIdMapping, notNullValue());
assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
assertThat(naturalIdMapping.getNaturalIdAttributes().get(0).getAttributeName(), is("assignment"));
assertThat(naturalIdMapping.getNaturalIdAttributes().get(1).getAttributeName(), is("userCode"));
final ArrayList<AttributeMapping> attributeMappings = new ArrayList<>(entityMappingType.getAttributeMappings());
assertThat(attributeMappings.size(), is(5));
assertThat(attributeMappings.get(0).getAttributeName(), is("assignment"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[0], is("assignment"));
assertThat(attributeMappings.get(1).getAttributeName(), is("name"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[1], is("name"));
final EmbeddedAttributeMapping theComponentAttrMapping = (EmbeddedAttributeMapping) attributeMappings.get(2);
assertThat(theComponentAttrMapping.getAttributeName(), is("theComponent"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[2], is("theComponent"));
final EmbeddableMappingType embeddable = theComponentAttrMapping.getMappedType();
final ArrayList<AttributeMapping> embeddableAttributeMappings = new ArrayList<>(embeddable.getAttributeMappings());
assertThat(embeddableAttributeMappings.get(0).getAttributeName(), is("nestedAnything"));
assertThat(embeddableAttributeMappings.get(1).getAttributeName(), is("nestedName"));
assertThat(attributeMappings.get(3).getAttributeName(), is("theComponents"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[3], is("theComponents"));
assertThat(attributeMappings.get(4).getAttributeName(), is("userCode"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[4], is("userCode"));
}
Aggregations