1
resposta

Erro ao utilizar EntityManager

Estou utilizando o spring data e acessando os dados atraves de interface que extendem JpaRepository. Preciso agora acessar os dados utilizando query nativa e para isso preciso acessar os dados utilizando o EntityManager. O EntityManager utiliza as mesmas configuracoes de conexao com a fonte de dados definidas no application.properties? Criei uma nova classe para acessar desta forma :

@Repository
public class SaldoDeContaContabilDAO {

    @PersistenceContext
    private EntityManager em;

    public List<SaldoDeContaContabil> toList(VisaoContabil visao) {
        String sql =  " SELECT CODPLACTA, " +
                      "        ABS (SALDOINICIAL) AS SALDOINICIAL," + 
                      "        NATUREZASALDOINICIAL," + 
                      "        ABS (DEBITO) AS DEBITO," + 
                      "        ABS (CREDITO) AS CREDITO, "+ 

        ... continua...
}

Mas esta retornando erro ao executá-la.

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

   DataSourceAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition)

   DataSourceAutoConfiguration.PooledDataSourceConfiguration matched:
      - AnyNestedCondition 1 matched 1 did not; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailable PooledDataSource found supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (DataSourceAutoConfiguration.PooledDataSourceCondition)
      - @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)

   DataSourceConfiguration.Hikari matched:
      - @ConditionalOnClass found required class 'com.zaxxer.hikari.HikariDataSource' (OnClassCondition)
      - @ConditionalOnProperty (spring.datasource.type=com.zaxxer.hikari.HikariDataSource) matched (OnPropertyCondition)
      - @ConditionalOnMissingBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)

...CONTINUA...
2019-10-29 20:47:23 ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@12d3a4e9] to prepare test instance [br.com.atac.erp.model.service.SaldoDeContaContabilServiceTest@7ea37dbf]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'br.com.atac.erp.model.service.SaldoDeContaContabilServiceTest': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'br.com.atac.erp.model.service.SaldoDeContaContabilService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376)
1 resposta

O spring não conseguiu injetar a sua classe SaldoDeContaContabilService, você precisa fornecer instâncias do bean Spring de sua classe SaldoDeContaContabilService. Isso pode ser feito através da anotação @ContextConfiguration:

...
@ContextConfiguration(classes = {SaldoDeContaContabilService.class})
public class ApplicationTest {
...