use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class XmlAccessTest method assertAccessType.
// uses the first getter of the tupelizer for the assertions
private void assertAccessType(SessionFactoryImplementor factory, Class<?> classUnderTest, AccessType accessType) {
final EntityPersister entityDescriptor = factory.getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(classUnderTest.getName());
final Collection<AttributeMapping> attributeMappings = entityDescriptor.getAttributeMappings();
final AttributeMapping attributeMapping = attributeMappings.iterator().next();
final Getter accessGetter = attributeMapping.getPropertyAccess().getGetter();
if (AccessType.FIELD.equals(accessType)) {
assertTrue(accessGetter instanceof GetterFieldImpl, "Field access was expected.");
} else {
assertTrue(accessGetter instanceof GetterMethodImpl, "Property access was expected.");
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class EnhancementAsProxyLazinessInterceptor method handleRead.
@Override
protected Object handleRead(Object target, String attributeName, Object value) {
// it is illegal for this interceptor to still be attached to the entity after initialization
if (isInitialized()) {
throw new IllegalStateException("EnhancementAsProxyLazinessInterceptor interception on an initialized instance");
}
// - we already know the id, return that
if (identifierAttributeNames.contains(attributeName)) {
return extractIdValue(target, attributeName);
}
// Use `performWork` to group together multiple Session accesses
return EnhancementHelper.performWork(this, (session, isTempSession) -> {
final Object[] writtenValues;
final EntityPersister entityPersister = session.getFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(getEntityName());
if (writtenFieldNames != null && !writtenFieldNames.isEmpty()) {
if (writtenFieldNames.contains(attributeName)) {
// the requested attribute was one of the attributes explicitly set, we can just return the explicitly set value
return entityPersister.getPropertyValue(target, attributeName);
}
// otherwise we want to save all of the explicitly set values in anticipation of
// the force initialization below so that we can "replay" them after the
// initialization
writtenValues = new Object[writtenFieldNames.size()];
int index = 0;
for (String writtenFieldName : writtenFieldNames) {
writtenValues[index] = entityPersister.getPropertyValue(target, writtenFieldName);
index++;
}
} else {
writtenValues = null;
}
final Object initializedValue = forceInitialize(target, attributeName, session, isTempSession);
setInitialized();
if (writtenValues != null) {
// here is the replaying of the explicitly set values we prepared above
for (String writtenFieldName : writtenFieldNames) {
List<AttributeMapping> attributeMappings = entityPersister.getAttributeMappings();
for (int index = 0; index < attributeMappings.size(); index++) {
if (writtenFieldName.contains(attributeMappings.get(index).getAttributeName())) {
entityPersister.setValue(target, index, writtenValues[index]);
}
}
}
writtenFieldNames.clear();
}
return initializedValue;
}, getEntityName(), attributeName);
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class Builders method attributeResult.
public static ResultBuilder attributeResult(String columnAlias, String entityName, String attributePath, SessionFactoryImplementor sessionFactory) {
if (attributePath.contains(".")) {
throw new NotYetImplementedFor6Exception("Support for defining a NativeQuery attribute result based on a composite path is not yet implemented");
}
final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
final String fullEntityName = runtimeMetamodels.getMappingMetamodel().getImportedName(entityName);
final EntityPersister entityMapping = runtimeMetamodels.getMappingMetamodel().findEntityDescriptor(fullEntityName);
if (entityMapping == null) {
throw new IllegalArgumentException("Could not locate entity mapping : " + fullEntityName);
}
final AttributeMapping attributeMapping = entityMapping.findAttributeMapping(attributePath);
if (attributeMapping == null) {
throw new IllegalArgumentException("Could not locate attribute mapping : " + fullEntityName + "." + attributePath);
}
if (attributeMapping instanceof SingularAttributeMapping) {
final SingularAttributeMapping singularAttributeMapping = (SingularAttributeMapping) attributeMapping;
return new DynamicResultBuilderAttribute(singularAttributeMapping, columnAlias, fullEntityName, attributePath);
}
throw new IllegalArgumentException(String.format(Locale.ROOT, "Specified attribute mapping [%s.%s] not a basic attribute: %s", fullEntityName, attributePath, attributeMapping));
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method transformDurationArithmetic.
private Object transformDurationArithmetic(SqmBinaryArithmetic<?> expression) {
BinaryArithmeticOperator operator = expression.getOperator();
// the expression tree
switch(operator) {
case ADD:
case SUBTRACT:
// the only legal binary operations involving
// a duration with a date or timestamp are
// addition and subtraction with the duration
// on the right and the date or timestamp on
// the left, producing a date or timestamp
//
// ts + d or ts - d
//
// the only legal binary operations involving
// two durations are addition and subtraction,
// producing a duration
//
// d1 + d2
// re-express addition of non-leaf duration
// expressions to a date or timestamp as
// addition of leaf durations to a date or
// timestamp
// ts + x * (d1 + d2) => (ts + x * d1) + x * d2
// ts - x * (d1 + d2) => (ts - x * d1) - x * d2
// ts + x * (d1 - d2) => (ts + x * d1) - x * d2
// ts - x * (d1 - d2) => (ts - x * d1) + x * d2
Expression timestamp = adjustedTimestamp;
SqmExpressible<?> timestampType = adjustedTimestampType;
adjustedTimestamp = toSqlExpression(expression.getLeftHandOperand().accept(this));
JdbcMappingContainer type = adjustedTimestamp.getExpressionType();
if (type instanceof SqmExpressible) {
adjustedTimestampType = (SqmExpressible<?>) type;
} else if (type instanceof AttributeMapping) {
adjustedTimestampType = (SqmExpressible<?>) ((AttributeMapping) type).getMappedType();
} else {
// else we know it has not been transformed
adjustedTimestampType = expression.getLeftHandOperand().getNodeType();
}
if (operator == SUBTRACT) {
negativeAdjustment = !negativeAdjustment;
}
try {
return expression.getRightHandOperand().accept(this);
} finally {
if (operator == SUBTRACT) {
negativeAdjustment = !negativeAdjustment;
}
adjustedTimestamp = timestamp;
adjustedTimestampType = timestampType;
}
case MULTIPLY:
// finally, we can multiply a duration on the
// right by a scalar value on the left
// scalar multiplication produces a duration
// x * d
// distribute scalar multiplication over the
// terms, not forgetting the propagated scale
// x * (d1 + d2) => x * d1 + x * d2
// x * (d1 - d2) => x * d1 - x * d2
// -x * (d1 + d2) => - x * d1 - x * d2
// -x * (d1 - d2) => - x * d1 + x * d2
Expression duration = toSqlExpression(expression.getLeftHandOperand().accept(this));
Expression scale = adjustmentScale;
boolean negate = negativeAdjustment;
adjustmentScale = applyScale(duration);
// was sucked into the scale
negativeAdjustment = false;
try {
return expression.getRightHandOperand().accept(this);
} finally {
adjustmentScale = scale;
negativeAdjustment = negate;
}
default:
throw new SemanticException("illegal operator for a duration " + operator);
}
}
use of org.hibernate.metamodel.mapping.AttributeMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method setPropertyValues.
@Override
public void setPropertyValues(Object object, Object[] values) {
if (accessOptimizer != null) {
accessOptimizer.setPropertyValues(object, values);
} else {
if (hasSubclasses()) {
visitAttributeMappings(attribute -> {
final int stateArrayPosition = ((StateArrayContributorMapping) attribute).getStateArrayPosition();
final Object value = values[stateArrayPosition];
if (value != UNFETCHED_PROPERTY) {
final Setter setter = attribute.getPropertyAccess().getSetter();
setter.set(object, value);
}
});
} else {
visitFetchables(fetchable -> {
final AttributeMapping attribute = (AttributeMapping) fetchable;
final int stateArrayPosition = ((StateArrayContributorMapping) attribute).getStateArrayPosition();
final Object value = values[stateArrayPosition];
if (value != UNFETCHED_PROPERTY) {
final Setter setter = attribute.getPropertyAccess().getSetter();
setter.set(object, value);
}
}, null);
}
}
}
Aggregations