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();
}
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);
}
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;
}
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);
}
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);
}
Aggregations