Search in sources :

Example 11 with RelationalPersistentProperty

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();
}
Also used : RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 12 with RelationalPersistentProperty

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());
}
Also used : ValueFunction(org.springframework.data.relational.core.query.ValueFunction) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) SQLType(java.sql.SQLType) JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 13 with RelationalPersistentProperty

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);
}
Also used : RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) SQLType(java.sql.SQLType)

Example 14 with RelationalPersistentProperty

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));
}
Also used : DbActionExecutionResult(org.springframework.data.relational.core.conversion.DbActionExecutionResult) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 15 with RelationalPersistentProperty

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;
    }
}
Also used : SqlGenerator(org.springframework.data.jdbc.core.convert.SqlGenerator) PersistentProperty(org.springframework.data.mapping.PersistentProperty) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Aggregations

RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)22 Test (org.junit.jupiter.api.Test)9 PersistentPropertyPathExtension (org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)5 BasicRelationalPersistentProperty (org.springframework.data.relational.core.mapping.BasicRelationalPersistentProperty)3 SQLType (java.sql.SQLType)2 ArrayList (java.util.ArrayList)2 SoftAssertions (org.assertj.core.api.SoftAssertions)2 RelationalPersistentEntity (org.springframework.data.relational.core.mapping.RelationalPersistentEntity)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 SqlSession (org.apache.ibatis.session.SqlSession)1 Assertions (org.assertj.core.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 ArgumentMatchers (org.mockito.ArgumentMatchers)1 Mockito (org.mockito.Mockito)1 Example (org.springframework.data.domain.Example)1 ExampleMatcher (org.springframework.data.domain.ExampleMatcher)1