Search in sources :

Example 6 with JdbcPersistentProperty

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method delete.

@Override
public void delete(Object rootId, PropertyPath propertyPath) {
    JdbcPersistentEntity<?> rootEntity = context.getRequiredPersistentEntity(propertyPath.getOwningType());
    JdbcPersistentProperty referencingProperty = rootEntity.getRequiredPersistentProperty(propertyPath.getSegment());
    Assert.notNull(referencingProperty, "No property found matching the PropertyPath " + propertyPath);
    String format = sql(rootEntity.getType()).createDeleteByPath(propertyPath);
    HashMap<String, Object> parameters = new HashMap<>();
    parameters.put("rootId", rootId);
    operations.update(format, parameters);
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) HashMap(java.util.HashMap)

Example 7 with JdbcPersistentProperty

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method getPropertyMap.

private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersistentEntity<S> persistentEntity) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
        if (!property.isEntity()) {
            Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
            Object convertedValue = convert(value, property.getColumnType());
            parameters.addValue(property.getColumnName(), convertedValue, JdbcUtil.sqlTypeFor(property.getColumnType()));
        }
    });
    return parameters;
}
Also used : NonTransientDataAccessException(org.springframework.dao.NonTransientDataAccessException) EntityInformation(org.springframework.data.repository.core.EntityInformation) HashMap(java.util.HashMap) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) JdbcUtil(org.springframework.data.jdbc.support.JdbcUtil) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) KeyHolder(org.springframework.jdbc.support.KeyHolder) Collectors(java.util.stream.Collectors) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations) PropertyHandler(org.springframework.data.mapping.PropertyHandler) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Map(java.util.Map) BasicJdbcPersistentEntityInformation(org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation) JdbcPersistentEntity(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity) RowMapper(org.springframework.jdbc.core.RowMapper) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) PropertyPath(org.springframework.data.mapping.PropertyPath) JdbcPersistentEntityInformation(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation) JdbcMappingContext(org.springframework.data.jdbc.mapping.model.JdbcMappingContext) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Assert(org.springframework.util.Assert) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Example 8 with JdbcPersistentProperty

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.

the class EntityRowMapper method mapRow.

/*
	 * (non-Javadoc)
	 * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
	 */
@Override
public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {
    T result = createInstance(resultSet);
    ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(result), conversions);
    Object id = idProperty == null ? null : readFrom(resultSet, idProperty, "");
    for (JdbcPersistentProperty property : entity) {
        if (property.isCollectionLike()) {
            propertyAccessor.setProperty(property, accessStrategy.findAllByProperty(id, property));
        } else if (property.isMap()) {
            Iterable<Object> allByProperty = accessStrategy.findAllByProperty(id, property);
            propertyAccessor.setProperty(property, ITERABLE_OF_ENTRY_TO_MAP_CONVERTER.convert(allByProperty));
        } else {
            propertyAccessor.setProperty(property, readFrom(resultSet, property, ""));
        }
    }
    return result;
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor)

Example 9 with JdbcPersistentProperty

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.

the class SqlGenerator method createDeleteAllSql.

String createDeleteAllSql(PropertyPath path) {
    if (path == null) {
        return String.format("DELETE FROM %s", entity.getTableName());
    }
    JdbcPersistentEntity<?> entityToDelete = context.getRequiredPersistentEntity(path.getLeafType());
    JdbcPersistentEntity<?> owningEntity = context.getRequiredPersistentEntity(path.getOwningType());
    JdbcPersistentProperty property = owningEntity.getRequiredPersistentProperty(path.getSegment());
    String innerMostCondition = String.format("%s IS NOT NULL", property.getReverseColumnName());
    String condition = cascadeConditions(innerMostCondition, path.next());
    return String.format("DELETE FROM %s WHERE %s", entityToDelete.getTableName(), condition);
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)

Example 10 with JdbcPersistentProperty

use of org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty in project spring-data-jdbc by spring-projects.

the class MyBatisDataAccessStrategyUnitTests method findAllByProperty.

@SuppressWarnings("unchecked")
// DATAJDBC-123
@Test
public void findAllByProperty() {
    JdbcPersistentProperty property = mock(JdbcPersistentProperty.class, Mockito.RETURNS_DEEP_STUBS);
    when(property.getOwner().getType()).thenReturn((Class) String.class);
    doReturn(Number.class).when(property).getType();
    doReturn("propertyName").when(property).getName();
    accessStrategy.findAllByProperty("id", property);
    verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty-propertyName"), captor.capture());
    // 
    assertThat(captor.getValue()).isNotNull().extracting(// 
    MyBatisContext::getInstance, // 
    MyBatisContext::getId, // 
    MyBatisContext::getDomainType, // 
    c -> c.get("key")).containsExactly(// 
    null, // 
    "id", // 
    Number.class, // 
    null);
}
Also used : Arrays(java.util.Arrays) Test(org.junit.Test) MyBatisDataAccessStrategy(org.springframework.data.jdbc.mybatis.MyBatisDataAccessStrategy) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) MyBatisContext(org.springframework.data.jdbc.mybatis.MyBatisContext) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Assertions(org.assertj.core.api.Assertions) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) PropertyPath(org.springframework.data.mapping.PropertyPath) Collections(java.util.Collections) SqlSession(org.apache.ibatis.session.SqlSession) Before(org.junit.Before) JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) Test(org.junit.Test)

Aggregations

JdbcPersistentProperty (org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)10 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JdbcPersistentEntity (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)2 PropertyPath (org.springframework.data.mapping.PropertyPath)2 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)2 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)2 KeyHolder (org.springframework.jdbc.support.KeyHolder)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 SqlSession (org.apache.ibatis.session.SqlSession)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 Assertions (org.assertj.core.api.Assertions)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1