use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.
the class RelationalEntityVersionUtils method setVersionNumberOnEntity.
/**
* Set the version property on an instance of a relational persistent entity. This method returns an instance of the
* same type with the updated version property and will correctly handle the case where the version is immutable.
*
* @param instance must not be {@literal null}.
* @param version The value to be set on the version property.
* @param persistentEntity must not be {@literal null}.
* @param converter must not be {@literal null}.
* @return An instance of the entity with an updated version property.
* @throws IllegalArgumentException if the entity does not have a version property.
*/
public static <S> S setVersionNumberOnEntity(S instance, @Nullable Number version, RelationalPersistentEntity<S> persistentEntity, RelationalConverter converter) {
if (!persistentEntity.hasVersionProperty()) {
throw new IllegalArgumentException("The entity does not have a version property.");
}
PersistentPropertyAccessor<S> propertyAccessor = converter.getPropertyAccessor(persistentEntity, instance);
RelationalPersistentProperty versionProperty = persistentEntity.getRequiredVersionProperty();
propertyAccessor.setProperty(versionProperty, version);
return propertyAccessor.getBean();
}
use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.
the class QueryMapper method mapCondition.
private Condition mapCondition(CriteriaDefinition criteria, MapSqlParameterSource parameterSource, Table table, @Nullable RelationalPersistentEntity<?> entity) {
Field propertyField = createPropertyField(entity, criteria.getColumn(), this.mappingContext);
// Single embedded entity
if (propertyField.isEmbedded()) {
return mapEmbeddedObjectCondition(criteria, parameterSource, table, ((MetadataBackedField) propertyField).getPath().getLeafProperty());
}
TypeInformation<?> actualType = propertyField.getTypeHint().getRequiredActualType();
Column column = table.column(propertyField.getMappedColumnName());
Object mappedValue;
SQLType sqlType;
if (criteria.getValue() instanceof JdbcValue) {
JdbcValue settableValue = (JdbcValue) criteria.getValue();
mappedValue = convertValue(settableValue.getValue(), propertyField.getTypeHint());
sqlType = getTypeHint(mappedValue, actualType.getType(), settableValue);
} else if (criteria.getValue() instanceof ValueFunction) {
ValueFunction<Object> valueFunction = (ValueFunction<Object>) criteria.getValue();
Object value = valueFunction.apply(getEscaper(criteria.getComparator()));
mappedValue = convertValue(value, propertyField.getTypeHint());
sqlType = propertyField.getSqlType();
} else if (//
propertyField instanceof MetadataBackedField && //
((MetadataBackedField) propertyField).property != null && (criteria.getValue() == null || !criteria.getValue().getClass().isArray())) {
RelationalPersistentProperty property = ((MetadataBackedField) propertyField).property;
JdbcValue jdbcValue = convertToJdbcValue(property, criteria.getValue());
mappedValue = jdbcValue.getValue();
sqlType = jdbcValue.getJdbcType() != null ? jdbcValue.getJdbcType() : propertyField.getSqlType();
} else {
mappedValue = convertValue(criteria.getValue(), propertyField.getTypeHint());
sqlType = propertyField.getSqlType();
}
return createCondition(column, mappedValue, sqlType, parameterSource, criteria.getComparator(), criteria.isIgnoreCase());
}
use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.
the class QueryMapper method mapEmbeddedObjectCondition.
private Condition mapEmbeddedObjectCondition(CriteriaDefinition criteria, MapSqlParameterSource parameterSource, Table table, RelationalPersistentProperty embeddedProperty) {
RelationalPersistentEntity<?> persistentEntity = this.mappingContext.getRequiredPersistentEntity(embeddedProperty);
Assert.isInstanceOf(persistentEntity.getType(), criteria.getValue(), () -> "Value must be of type " + persistentEntity.getType().getName() + " for embedded entity matching");
PersistentPropertyAccessor<Object> embeddedAccessor = persistentEntity.getPropertyAccessor(criteria.getValue());
String prefix = embeddedProperty.getEmbeddedPrefix();
Condition condition = null;
for (RelationalPersistentProperty nestedProperty : persistentEntity) {
SqlIdentifier sqlIdentifier = nestedProperty.getColumnName().transform(prefix::concat);
Object mappedNestedValue = convertValue(embeddedAccessor.getProperty(nestedProperty), nestedProperty.getTypeInformation());
SQLType sqlType = converter.getTargetSqlType(nestedProperty);
Condition mappedCondition = createCondition(table.column(sqlIdentifier), mappedNestedValue, sqlType, parameterSource, criteria.getComparator(), criteria.isIgnoreCase());
if (condition != null) {
condition = condition.and(mappedCondition);
} else {
condition = mappedCondition;
}
}
return Conditions.nest(condition);
}
use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.
the class JdbcAggregateChangeExecutionContext method executeInsertRoot.
<T> void executeInsertRoot(DbAction.InsertRoot<T> insert) {
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(insert.getEntityType());
Object id;
if (persistentEntity.hasVersionProperty()) {
RelationalPersistentProperty versionProperty = persistentEntity.getVersionProperty();
Assert.state(versionProperty != null, "Version property must not be null at this stage.");
long initialVersion = versionProperty.getActualType().isPrimitive() ? 1L : 0;
T rootEntity = //
RelationalEntityVersionUtils.setVersionNumberOnEntity(insert.getEntity(), initialVersion, persistentEntity, converter);
id = accessStrategy.insert(rootEntity, insert.getEntityType(), Identifier.empty());
setNewVersion(initialVersion);
} else {
id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), Identifier.empty());
}
add(new DbActionExecutionResult(insert, id));
}
use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.
the class DefaultDataAccessStrategy method insert.
/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
*/
@Override
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
SqlGenerator sqlGenerator = sql(domainType);
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
SqlIdentifierParameterSource parameterSource = getParameterSource(instance, persistentEntity, "", PersistentProperty::isIdProperty, getIdentifierProcessing());
identifier.forEach((name, value, type) -> addConvertedPropertyValue(parameterSource, name, value, type));
Object idValue = getIdValueOrNull(instance, persistentEntity);
if (idValue != null) {
RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
addConvertedPropertyValue(parameterSource, idProperty, idValue, idProperty.getColumnName());
}
String insertSql = sqlGenerator.getInsert(new HashSet<>(parameterSource.getIdentifiers()));
if (idValue == null) {
return executeInsertAndReturnGeneratedId(domainType, persistentEntity, parameterSource, insertSql);
} else {
operations.update(insertSql, parameterSource);
return null;
}
}
Aggregations