use of org.hibernate.metamodel.mapping.SingularAttributeMapping in project hibernate-orm by hibernate.
the class AbstractCompositeIdAndNaturalIdTest method testNaturalIdNullability.
@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
final EntityMappingType accountMapping = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Account.class);
final SingularAttributeMapping shortCodeMapping = ((SimpleNaturalIdMapping) accountMapping.getNaturalIdMapping()).getAttribute();
final AttributeMetadata shortCodeMetadata = shortCodeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertThat(shortCodeMetadata.isNullable(), is(false));
final EntityPersister rootEntityPersister = accountMapping.getRootEntityDescriptor().getEntityPersister();
final int shortCodeLegacyPropertyIndex = rootEntityPersister.getEntityMetamodel().getPropertyIndex("shortCode");
assertThat(shortCodeLegacyPropertyIndex, is(0));
assertThat(rootEntityPersister.getPropertyNullability()[shortCodeLegacyPropertyIndex], is(false));
}
use of org.hibernate.metamodel.mapping.SingularAttributeMapping in project hibernate-orm by hibernate.
the class ImmutableManyToOneNaturalIdAnnotationTest method testNaturalIdNullability.
@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
// nullability is not specified for either properties making up
// the natural ID, so they should be nullable by annotation-specific default
final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
final EntityMappingType childMapping = runtimeMetamodels.getEntityMappingType(Child.class.getName());
final EntityPersister persister = childMapping.getEntityPersister();
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final int nameIndex = entityMetamodel.getPropertyIndex("name");
final int parentIndex = entityMetamodel.getPropertyIndex("parent");
// checking alphabetic sort in relation to EntityPersister/EntityMetamodel
assertThat(nameIndex, lessThan(parentIndex));
assertFalse(persister.getPropertyUpdateability()[nameIndex]);
assertFalse(persister.getPropertyUpdateability()[parentIndex]);
assertTrue(persister.getPropertyNullability()[nameIndex]);
assertTrue(persister.getPropertyNullability()[parentIndex]);
final NaturalIdMapping naturalIdMapping = childMapping.getNaturalIdMapping();
assertNotNull(naturalIdMapping);
assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
// access by list-index should again be alphabetically sorted
final SingularAttributeMapping first = naturalIdMapping.getNaturalIdAttributes().get(0);
assertThat(first.getAttributeName(), is("name"));
final AttributeMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertFalse(firstMetadata.getMutabilityPlan().isMutable());
final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
assertThat(second.getAttributeName(), is("parent"));
final AttributeMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
use of org.hibernate.metamodel.mapping.SingularAttributeMapping in project hibernate-orm by hibernate.
the class CompoundNaturalIdMapping method forEachJdbcValue.
@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
assert value instanceof Object[];
final Object[] incoming = (Object[]) value;
assert incoming.length == attributes.size();
int span = 0;
for (int i = 0; i < attributes.size(); i++) {
final SingularAttributeMapping attribute = attributes.get(i);
span += attribute.forEachJdbcValue(incoming[i], clause, span + offset, valuesConsumer, session);
}
return span;
}
use of org.hibernate.metamodel.mapping.SingularAttributeMapping in project hibernate-orm by hibernate.
the class CompoundNaturalIdMapping method extractNaturalIdFromEntityState.
@Override
public Object[] extractNaturalIdFromEntityState(Object[] state, SharedSessionContractImplementor session) {
if (state == null) {
return null;
}
if (state.length == attributes.size()) {
return state;
}
final Object[] values = new Object[attributes.size()];
for (int i = 0; i <= attributes.size() - 1; i++) {
final SingularAttributeMapping attributeMapping = attributes.get(i);
values[i] = state[attributeMapping.getStateArrayPosition()];
}
return values;
}
use of org.hibernate.metamodel.mapping.SingularAttributeMapping in project hibernate-orm by hibernate.
the class CompoundNaturalIdMapping method verifyFlushState.
@Override
public void verifyFlushState(Object id, Object[] currentState, Object[] loadedState, SharedSessionContractImplementor session) {
if (isMutable()) {
// the natural id is mutable (!immutable), no need to do the checks
return;
}
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
final EntityPersister persister = getDeclaringType().getEntityPersister();
final Object[] naturalId = extractNaturalIdFromEntityState(currentState, session);
final Object snapshot = loadedState == null ? persistenceContext.getNaturalIdSnapshot(id, persister) : persister.getNaturalIdMapping().extractNaturalIdFromEntityState(loadedState, session);
final Object[] previousNaturalId = (Object[]) snapshot;
assert naturalId.length == getNaturalIdAttributes().size();
assert previousNaturalId.length == naturalId.length;
for (int i = 0; i < getNaturalIdAttributes().size(); i++) {
final SingularAttributeMapping attributeMapping = getNaturalIdAttributes().get(i);
final boolean updatable = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(persister).isUpdatable();
if (updatable) {
// property is updatable (mutable), there is nothing to check
continue;
}
final Object currentValue = naturalId[i];
final Object previousValue = previousNaturalId[i];
if (!attributeMapping.areEqual(currentValue, previousValue, session)) {
throw new HibernateException(String.format("An immutable attribute [%s] within compound natural identifier of entity %s was altered from `%s` to `%s`", attributeMapping.getAttributeName(), persister.getEntityName(), previousValue, currentValue));
}
}
}
Aggregations