Announcement Announcement Module
Collapse
No announcement yet.
How do you instantiate a repository? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do you instantiate a repository?



    Hello,

    I am currently trying to connect to a database using Spring Data and Hibernate classes which a colleague of mine created, in a simple Java class called App.

    I have created an ApplicationConfig class:

    @Configuration
    @EnableJpaRepositories
    @ComponentScan
    @Import(InfrastructureConfig.class)
    public class ApplicationConfig {

    }
    ... an InfrastructureConfig class:

    public class InfrastructureConfig {

    @Bean
    public DataSource dataSource() {

    ComboPooledDataSource source = new ComboPooledDataSource();
    try {
    source.setDriverClass("oracle.jdbc.driver.OracleDr iver");
    } catch (PropertyVetoException e) {
    e.printStackTrace();
    }
    source.setJdbcUrl("jdbc:oracle:thin:@oracledb-test.xxx.com:1234:AAAA");
    source.setUser("dbo1");
    source.setPassword("xxxxx");

    return source;

    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    // Same as in application-context.xml
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setDatabase(Database.ORACLE);
    vendorAdapter.setShowSql(true);
    vendorAdapter.setGenerateDdl(false);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);

    // In Spring 3.1, you don't need persistence.xml anymore if you use the line below
    factory.setPackagesToScan("com.xxxx.abc.model");
    factory.setDataSource(dataSource());

    return factory;

    }

    @Bean
    public PlatformTransactionManager transactionManager() {

    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFac tory().getObject());
    return txManager;

    }


    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
    return new HibernateExceptionTranslator();
    }
    ... and a repository class:

    import org.springframework.data.repository.CrudRepository ;

    public interface DlvparamRepository extends CrudRepository<Dlvparam, DlvparamPK> {

    // Don't need to add any methods here as Spring Data automatically generates them

    }

    I have been trying to access the repository using an autowired static variable in the App class:

    @Autowired
    DlvparamRepository dlvparamRepository;
    ... but to no avail, as it is always null.

    How do you instantiate a repository?

    Many thanks.

    Philroc






















  • #2
    I have added the following code to my App class's main method, to create a context:

    ApplicationContext context =
    new AnnotationConfigApplicationContext("ApplicationCon fig.class");
    assertThat(context, is(notNullValue()));
    assertThat(context.getBean(DlvparamRepository.clas s), is(notNullValue()));
    When I run my program, I get an exception stating that "No qualifying bean of type [....repositories.DlvparamRepository] is defined", although it is defined as shown above.

    Comment


    • #3
      I hve solved the problem by adding replacing

      @EnableJpaRepositories
      in the ApplicationConfig class

      by

      @EnableJpaRepositories(basePackages="com.xxxx.yyy. zzz.repositories")

      Comment


      • #4
        Although I can now access the repository in a @Test method, I can't access it in App class's main method.

        For instance, when I run the following code :

        ApplicationContext context =
        new AnnotationConfigApplicationContext("com.xxx.distri bution.pressrunsheets.jpa.ApplicationConfig.class ");

        DlvparamRepository dlvparamRepository =
        context.getBean(DlvparamRepository.class);

        LOGGER.info("dlvparamRepository = " + dlvparamRepository);
        the following message is displayed

        Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefini tionException: No qualifying bean of type [com.xxx.distribution.pressrunsheets.repositories.D lvparamRepository] is defined

        Comment


        • #5
          Hello again,

          I have found the problem: the AppliConfig.class name should not be between quotation marks.

          Case closed.

          Comment

          Working...
          X