Search in sources :

Example 6 with JdbcMappingContext

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

the class AbstractJdbcConfiguration method jdbcMappingContext.

/**
 * Register a {@link JdbcMappingContext} and apply an optional {@link NamingStrategy}.
 *
 * @param namingStrategy optional {@link NamingStrategy}. Use {@link NamingStrategy#INSTANCE} as fallback.
 * @param customConversions see {@link #jdbcCustomConversions()}.
 * @return must not be {@literal null}.
 */
@Bean
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, JdbcCustomConversions customConversions) {
    JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(NamingStrategy.INSTANCE));
    mappingContext.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
    return mappingContext;
}
Also used : JdbcMappingContext(org.springframework.data.jdbc.core.mapping.JdbcMappingContext) Bean(org.springframework.context.annotation.Bean)

Example 7 with JdbcMappingContext

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

the class EntityRowMapperUnitTests method createRowMapper.

@SuppressWarnings("unchecked")
private <T> EntityRowMapper<T> createRowMapper(Class<T> type, NamingStrategy namingStrategy) {
    RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
    DataAccessStrategy accessStrategy = mock(DataAccessStrategy.class);
    // the ID of the entity is used to determine what kind of ResultSet is needed for subsequent selects.
    Set<Trivial> trivials = // 
    Stream.of(// 
    new Trivial(1L, "one"), // 
    new Trivial(2L, "two")).collect(Collectors.toSet());
    Set<Map.Entry<Integer, Trivial>> simpleEntriesWithInts = trivials.stream().collect(Collectors.toMap(it -> it.getId().intValue(), Function.identity())).entrySet();
    Set<Map.Entry<String, Trivial>> simpleEntriesWithStringKeys = trivials.stream().collect(Collectors.toMap(Trivial::getName, Function.identity())).entrySet();
    doReturn(trivials).when(accessStrategy).findAllByPath(identifierOfValue(ID_FOR_ENTITY_NOT_REFERENCING_MAP), any(PersistentPropertyPath.class));
    doReturn(simpleEntriesWithStringKeys).when(accessStrategy).findAllByPath(identifierOfValue(ID_FOR_ENTITY_REFERENCING_MAP), any(PersistentPropertyPath.class));
    doReturn(simpleEntriesWithInts).when(accessStrategy).findAllByPath(identifierOfValue(ID_FOR_ENTITY_REFERENCING_LIST), any(PersistentPropertyPath.class));
    doReturn(trivials).when(accessStrategy).findAllByPath(identifierOfValue(ID_FOR_ENTITY_NOT_REFERENCING_MAP), any(PersistentPropertyPath.class));
    doReturn(simpleEntriesWithStringKeys).when(accessStrategy).findAllByPath(identifierOfValue(ID_FOR_ENTITY_REFERENCING_MAP), any(PersistentPropertyPath.class));
    doReturn(simpleEntriesWithInts).when(accessStrategy).findAllByPath(identifierOfValue(ID_FOR_ENTITY_REFERENCING_LIST), any(PersistentPropertyPath.class));
    BasicJdbcConverter converter = new BasicJdbcConverter(context, accessStrategy, new JdbcCustomConversions(), JdbcTypeFactory.unsupported(), IdentifierProcessing.ANSI);
    return new // 
    EntityRowMapper<>(// 
    (RelationalPersistentEntity<T>) context.getRequiredPersistentEntity(type), // 
    converter);
}
Also used : RelationalMappingContext(org.springframework.data.relational.core.mapping.RelationalMappingContext) PersistentPropertyPath(org.springframework.data.mapping.PersistentPropertyPath) JdbcMappingContext(org.springframework.data.jdbc.core.mapping.JdbcMappingContext)

Example 8 with JdbcMappingContext

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

the class SqlGeneratorFixedNamingStrategyUnitTests method configureSqlGenerator.

/**
 * Plug in a custom {@link NamingStrategy} for this test case.
 */
private SqlGenerator configureSqlGenerator(NamingStrategy namingStrategy) {
    RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
    JdbcConverter converter = new BasicJdbcConverter(context, (identifier, path) -> {
        throw new UnsupportedOperationException();
    });
    RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
    return new SqlGenerator(context, converter, persistentEntity, AnsiDialect.INSTANCE);
}
Also used : JdbcMappingContext(org.springframework.data.jdbc.core.mapping.JdbcMappingContext) RelationalMappingContext(org.springframework.data.relational.core.mapping.RelationalMappingContext)

Aggregations

JdbcMappingContext (org.springframework.data.jdbc.core.mapping.JdbcMappingContext)8 RelationalMappingContext (org.springframework.data.relational.core.mapping.RelationalMappingContext)5 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 Bean (org.springframework.context.annotation.Bean)2 BasicJdbcConverter (org.springframework.data.jdbc.core.convert.BasicJdbcConverter)2 DefaultDataAccessStrategy (org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy)2 Dialect (org.springframework.data.relational.core.dialect.Dialect)2 NamedParameterJdbcOperations (org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations)2 Supplier (java.util.function.Supplier)1 Assertions (org.assertj.core.api.Assertions)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 Answers (org.mockito.Answers)1 Mock (org.mockito.Mock)1 Mockito (org.mockito.Mockito)1 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)1 MockitoSettings (org.mockito.junit.jupiter.MockitoSettings)1 Strictness (org.mockito.quality.Strictness)1 Answer (org.mockito.stubbing.Answer)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1