0

I'm working on a big project written with java8 and SringBoot 2.2.6. The project uses Envers and, the girl builds the architecture say to me that she doesn't manage to put in the application.properties the Envers configuration. Than she do as follows:

@Configuration
public class JPAConfig {

 @Autowired
 private DataSource dataSource;

 @Bean(name="entityManagerFactory")
 public LocalSessionFactoryBean sessionFactory() throws IOException {
    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    factoryBean.setHibernateProperties(getHibernateProperties());
    factoryBean.setDataSource(dataSource);
    factoryBean.setPackagesToScan("it.xxxx.xxxxx.xxxxx.common.model");
    return factoryBean;
 }

 @Bean
 public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
 }

 private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", PostgreSQL82Dialect.class.getName());
    properties.put("hibernate.default_schema", "test");
    properties.put("hibernate.listeners.envers.autoRegister", true);
    properties.put("org.hibernate.envers.revision_field_name", "rev");
    properties.put("org.hibernate.envers.revision_type_field_name", "rev_type");
    properties.put("org.hibernate.envers.audit_table_prefix", "aud_");
    properties.put("org.hibernate.envers.store_data_at_delete", true);
    properties.put("org.hibernate.envers.audit_table_suffix", "");
    
    return properties;
 }

}

Problem is that without dataSource class name I can't start my @SpringBootTest classes and I don't know how to add it in a scenario like this (without change the configuration I mean).

I also tries to add this row inside the application.properties:

spring.profiles.active=@spring.profile@
spring.datasource.driver-class-name=org.postgresql.Driver
#JPA
spring.datasource.jndi-name=jdbc/test

But doesn't work at all..

If I run the App with JUnit I obtain this error:

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/test'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

Can you help me??

Thanks a lot

CoderJammer
  • 599
  • 3
  • 8
  • 27

1 Answers1

0

You need to register your Datasource as JNDI resource in the spring-boot embedded tomcat.

You can add it as test scope configuration.

This answer shows how to register a JNDI resource: https://stackoverflow.com/a/26005740/5230585

Katy
  • 1,023
  • 7
  • 19