Search in sources :

Example 1 with DefaultDataAccessStrategy

use of org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy in project spring-data-jdbc by spring-projects.

the class JdbcRepositoryFactoryBean method afterPropertiesSet.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#afterPropertiesSet()
	 */
@Override
public void afterPropertiesSet() {
    Assert.state(this.mappingContext != null, "MappingContext is required and must not be null!");
    Assert.state(this.converter != null, "RelationalConverter is required and must not be null!");
    if (this.operations == null) {
        Assert.state(beanFactory != null, "If no JdbcOperations are set a BeanFactory must be available.");
        this.operations = beanFactory.getBean(NamedParameterJdbcOperations.class);
    }
    if (this.dataAccessStrategy == null) {
        Assert.state(beanFactory != null, "If no DataAccessStrategy is set a BeanFactory must be available.");
        this.dataAccessStrategy = // 
        this.beanFactory.getBeanProvider(DataAccessStrategy.class).getIfAvailable(() -> {
            Assert.state(this.dialect != null, "Dialect is required and must not be null!");
            SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(this.mappingContext, this.converter, this.dialect);
            return new DefaultDataAccessStrategy(sqlGeneratorSource, this.mappingContext, this.converter, this.operations);
        });
    }
    if (this.queryMappingConfiguration == null) {
        this.queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
    }
    if (beanFactory != null) {
        entityCallbacks = EntityCallbacks.create(beanFactory);
    }
    super.afterPropertiesSet();
}
Also used : DefaultDataAccessStrategy(org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy) SqlGeneratorSource(org.springframework.data.jdbc.core.convert.SqlGeneratorSource) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations)

Example 2 with DefaultDataAccessStrategy

use of org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy in project spring-data-jdbc by spring-projects.

the class EnableJdbcRepositoriesIntegrationTests method jdbcOperationsRef.

// DATAJDBC-293
@Test
public void jdbcOperationsRef() {
    NamedParameterJdbcOperations operations = (NamedParameterJdbcOperations) ReflectionUtils.getField(OPERATIONS, factoryBean);
    assertThat(operations).isNotSameAs(defaultOperations).isSameAs(qualifierJdbcOperations);
    DataAccessStrategy dataAccessStrategy = (DataAccessStrategy) ReflectionUtils.getField(DATA_ACCESS_STRATEGY, factoryBean);
    assertThat(dataAccessStrategy).isNotSameAs(defaultDataAccessStrategy).isSameAs(qualifierDataAccessStrategy);
}
Also used : DataAccessStrategy(org.springframework.data.jdbc.core.convert.DataAccessStrategy) DefaultDataAccessStrategy(org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultDataAccessStrategy

use of org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy in project spring-data-jdbc by spring-projects.

the class SimpleJdbcRepositoryEventsUnitTests method before.

@BeforeEach
public void before() {
    RelationalMappingContext context = new JdbcMappingContext();
    NamedParameterJdbcOperations operations = createIdGeneratingOperations();
    DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
    Dialect dialect = HsqlDbDialect.INSTANCE;
    JdbcConverter converter = new BasicJdbcConverter(context, delegatingDataAccessStrategy, new JdbcCustomConversions(), new DefaultJdbcTypeFactory(operations.getJdbcOperations()), dialect.getIdentifierProcessing());
    SqlGeneratorSource generatorSource = new SqlGeneratorSource(context, converter, dialect);
    this.dataAccessStrategy = spy(new DefaultDataAccessStrategy(generatorSource, context, converter, operations));
    delegatingDataAccessStrategy.setDelegate(dataAccessStrategy);
    doReturn(true).when(dataAccessStrategy).update(any(), any());
    JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, H2Dialect.INSTANCE, publisher, operations);
    this.repository = factory.getRepository(DummyEntityRepository.class);
}
Also used : BasicJdbcConverter(org.springframework.data.jdbc.core.convert.BasicJdbcConverter) RelationalMappingContext(org.springframework.data.relational.core.mapping.RelationalMappingContext) DefaultDataAccessStrategy(org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy) JdbcRepositoryFactory(org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations) JdbcCustomConversions(org.springframework.data.jdbc.core.convert.JdbcCustomConversions) JdbcMappingContext(org.springframework.data.jdbc.core.mapping.JdbcMappingContext) BasicJdbcConverter(org.springframework.data.jdbc.core.convert.BasicJdbcConverter) JdbcConverter(org.springframework.data.jdbc.core.convert.JdbcConverter) Dialect(org.springframework.data.relational.core.dialect.Dialect) H2Dialect(org.springframework.data.relational.core.dialect.H2Dialect) HsqlDbDialect(org.springframework.data.relational.core.dialect.HsqlDbDialect) DefaultJdbcTypeFactory(org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory) SqlGeneratorSource(org.springframework.data.jdbc.core.convert.SqlGeneratorSource) DelegatingDataAccessStrategy(org.springframework.data.jdbc.core.convert.DelegatingDataAccessStrategy) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with DefaultDataAccessStrategy

use of org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy in project spring-data-jdbc by spring-projects.

the class MyBatisDataAccessStrategy method createCombinedAccessStrategy.

/**
 * Create a {@link DataAccessStrategy} that first checks for queries defined by MyBatis and if it doesn't find one
 * uses a {@link DefaultDataAccessStrategy}
 */
public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingContext context, JdbcConverter converter, NamedParameterJdbcOperations operations, SqlSession sqlSession, NamespaceStrategy namespaceStrategy, Dialect dialect) {
    // the DefaultDataAccessStrategy needs a reference to the returned DataAccessStrategy. This creates a dependency
    // cycle. In order to create it, we need something that allows to defer closing the cycle until all the elements are
    // created. That is the purpose of the DelegatingAccessStrategy.
    DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
    MyBatisDataAccessStrategy myBatisDataAccessStrategy = new MyBatisDataAccessStrategy(sqlSession, dialect.getIdentifierProcessing());
    myBatisDataAccessStrategy.setNamespaceStrategy(namespaceStrategy);
    CascadingDataAccessStrategy cascadingDataAccessStrategy = new CascadingDataAccessStrategy(asList(myBatisDataAccessStrategy, delegatingDataAccessStrategy));
    SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(context, converter, dialect);
    DefaultDataAccessStrategy defaultDataAccessStrategy = new // 
    DefaultDataAccessStrategy(// 
    sqlGeneratorSource, // 
    context, // 
    converter, // 
    operations);
    delegatingDataAccessStrategy.setDelegate(defaultDataAccessStrategy);
    return cascadingDataAccessStrategy;
}
Also used : CascadingDataAccessStrategy(org.springframework.data.jdbc.core.convert.CascadingDataAccessStrategy) DefaultDataAccessStrategy(org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy) SqlGeneratorSource(org.springframework.data.jdbc.core.convert.SqlGeneratorSource) DelegatingDataAccessStrategy(org.springframework.data.jdbc.core.convert.DelegatingDataAccessStrategy)

Aggregations

DefaultDataAccessStrategy (org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy)4 SqlGeneratorSource (org.springframework.data.jdbc.core.convert.SqlGeneratorSource)3 NamedParameterJdbcOperations (org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations)3 DelegatingDataAccessStrategy (org.springframework.data.jdbc.core.convert.DelegatingDataAccessStrategy)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 BasicJdbcConverter (org.springframework.data.jdbc.core.convert.BasicJdbcConverter)1 CascadingDataAccessStrategy (org.springframework.data.jdbc.core.convert.CascadingDataAccessStrategy)1 DataAccessStrategy (org.springframework.data.jdbc.core.convert.DataAccessStrategy)1 DefaultJdbcTypeFactory (org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory)1 JdbcConverter (org.springframework.data.jdbc.core.convert.JdbcConverter)1 JdbcCustomConversions (org.springframework.data.jdbc.core.convert.JdbcCustomConversions)1 JdbcMappingContext (org.springframework.data.jdbc.core.mapping.JdbcMappingContext)1 JdbcRepositoryFactory (org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory)1 Dialect (org.springframework.data.relational.core.dialect.Dialect)1 H2Dialect (org.springframework.data.relational.core.dialect.H2Dialect)1 HsqlDbDialect (org.springframework.data.relational.core.dialect.HsqlDbDialect)1 RelationalMappingContext (org.springframework.data.relational.core.mapping.RelationalMappingContext)1