Search in sources :

Example 6 with RelationalPersistentProperty

use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.

the class BasicJdbcConverterUnitTests method referencesAreNotEntitiesAndGetStoredAsTheirId.

// DATAJDBC-221
@Test
public void referencesAreNotEntitiesAndGetStoredAsTheirId() {
    RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(DummyEntity.class);
    SoftAssertions softly = new SoftAssertions();
    RelationalPersistentProperty reference = entity.getRequiredPersistentProperty("reference");
    softly.assertThat(reference.isEntity()).isFalse();
    softly.assertThat(converter.getColumnType(reference)).isEqualTo(Long.class);
    softly.assertAll();
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) Test(org.junit.jupiter.api.Test)

Example 7 with RelationalPersistentProperty

use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.

the class BasicJdbcConverter method getEntityColumnType.

@Nullable
private Class<?> getEntityColumnType(Class<?> type) {
    RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(type);
    if (persistentEntity == null) {
        return null;
    }
    RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
    if (idProperty == null) {
        return null;
    }
    return getColumnType(idProperty);
}
Also used : RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) Nullable(org.springframework.lang.Nullable)

Example 8 with RelationalPersistentProperty

use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.

the class SqlGenerator method selectBuilder.

private SelectBuilder.SelectWhere selectBuilder(Collection<SqlIdentifier> keyColumns) {
    Table table = getTable();
    List<Expression> columnExpressions = new ArrayList<>();
    List<Join> joinTables = new ArrayList<>();
    for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext.findPersistentPropertyPaths(entity.getType(), p -> true)) {
        PersistentPropertyPathExtension extPath = new PersistentPropertyPathExtension(mappingContext, path);
        // add a join if necessary
        Join join = getJoin(extPath);
        if (join != null) {
            joinTables.add(join);
        }
        Column column = getColumn(extPath);
        if (column != null) {
            columnExpressions.add(column);
        }
    }
    for (SqlIdentifier keyColumn : keyColumns) {
        columnExpressions.add(table.column(keyColumn).as(keyColumn));
    }
    SelectBuilder.SelectAndFrom selectBuilder = StatementBuilder.select(columnExpressions);
    SelectBuilder.SelectJoin baseSelect = selectBuilder.from(table);
    for (Join join : joinTables) {
        baseSelect = baseSelect.leftOuterJoin(join.joinTable).on(join.joinColumn).equals(join.parentId);
    }
    return (SelectBuilder.SelectWhere) baseSelect;
}
Also used : PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 9 with RelationalPersistentProperty

use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapper method getMappedExample.

/**
 * Transform each property of the {@link Example}'s probe into a {@link Criteria} and assemble them into a
 * {@link Query}.
 *
 * @param example
 * @param entity
 * @return query
 */
private <T> Query getMappedExample(Example<T> example, RelationalPersistentEntity<?> entity) {
    Assert.notNull(example, "Example must not be null!");
    Assert.notNull(entity, "RelationalPersistentEntity must not be null!");
    PersistentPropertyAccessor<T> propertyAccessor = entity.getPropertyAccessor(example.getProbe());
    ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher());
    final List<Criteria> criteriaBasedOnProperties = new ArrayList<>();
    entity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
        if (matcherAccessor.isIgnoredPath(property.getName())) {
            return;
        }
        Optional<?> optionalConvertedPropValue = matcherAccessor.getValueTransformerForPath(property.getName()).apply(Optional.ofNullable(propertyAccessor.getProperty(property)));
        if (!optionalConvertedPropValue.isPresent()) {
            return;
        }
        Object convPropValue = optionalConvertedPropValue.get();
        boolean ignoreCase = matcherAccessor.isIgnoreCaseForPath(property.getName());
        String column = property.getName();
        switch(matcherAccessor.getStringMatcherForPath(property.getName())) {
            case DEFAULT:
            case EXACT:
                criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).is(convPropValue).ignoreCase(ignoreCase) : Criteria.where(column).is(convPropValue).ignoreCase(ignoreCase));
                break;
            case ENDING:
                criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).like("%" + convPropValue).ignoreCase(ignoreCase) : Criteria.where(column).like("%" + convPropValue).ignoreCase(ignoreCase));
                break;
            case STARTING:
                criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).like(convPropValue + "%").ignoreCase(ignoreCase) : Criteria.where(column).like(convPropValue + "%").ignoreCase(ignoreCase));
                break;
            case CONTAINING:
                criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).like("%" + convPropValue + "%").ignoreCase(ignoreCase) : Criteria.where(column).like("%" + convPropValue + "%").ignoreCase(ignoreCase));
                break;
            default:
                throw new IllegalStateException(example.getMatcher().getDefaultStringMatcher() + " is not supported!");
        }
    });
    // Criteria, assemble!
    Criteria criteria = Criteria.empty();
    for (Criteria propertyCriteria : criteriaBasedOnProperties) {
        if (example.getMatcher().isAllMatching()) {
            criteria = criteria.and(propertyCriteria);
        } else {
            criteria = criteria.or(propertyCriteria);
        }
    }
    return Query.query(criteria);
}
Also used : Query(org.springframework.data.relational.core.query.Query) MappingContext(org.springframework.data.mapping.context.MappingContext) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Example(org.springframework.data.domain.Example) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) List(java.util.List) Optional(java.util.Optional) Criteria(org.springframework.data.relational.core.query.Criteria) RelationalPersistentEntity(org.springframework.data.relational.core.mapping.RelationalPersistentEntity) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) Assert(org.springframework.util.Assert) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) Criteria(org.springframework.data.relational.core.query.Criteria) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 10 with RelationalPersistentProperty

use of org.springframework.data.relational.core.mapping.RelationalPersistentProperty in project spring-data-jdbc by spring-projects.

the class BasicRelationalConverterAggregateReferenceUnitTests method convertsFromAggregateReference.

// DATAJDBC-221
@Test
public void convertsFromAggregateReference() {
    final RelationalPersistentProperty property = entity.getRequiredPersistentProperty("reference");
    AggregateReference<Object, Integer> reference = AggregateReference.to(23);
    Object writeValue = converter.writeValue(reference, ClassTypeInformation.from(converter.getColumnType(property)));
    Assertions.assertThat(writeValue).isEqualTo(23L);
}
Also used : RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) Test(org.junit.jupiter.api.Test)

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