0

I was configuring Criteria API in DBConfiguration file and I have one table named as audit_log where I am logging the sign-in user with date-time, status and username.

Here is my DBConfiguration file setup

@Configuration
@ComponentScan
@EntityScan("com.package.scan")
@EnableJpaRepositories("com.package.scan")
/*@PropertySource("classpath:db-config.properties")*/
public class DBConfiguration {

    public DBConfiguration() {
        logger = LogManager.getLogger(DBConfiguration.class.getName());
    }



    @Bean
        public JdbcTemplate jdbcTemplate(DataSource dataSource)
        {
            return new JdbcTemplate(dataSource);
        }

    @Bean
    public DataSource dataSource() {
        logger.info("dataSource() invoked");

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/x2c");
        dataSource.setUsername("postgres");
        //dataSource.setPassword("P@ssw0rd");
        dataSource.setPassword("home"); 
        logger.info("dataSource = " + dataSource); 

        return dataSource;
    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "com.mphasis.x2c.services.admin" });

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        // vendorAdapter.set
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        return em;
    }

     @Bean
        public PlatformTransactionManager transactionManager() {
            final JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

            return transactionManager;
        }

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

    Properties additionalProperties() {
           Properties properties = new Properties();
           properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
           properties.setProperty(
             "hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

           return properties;
       }

}

Now when I use create-drop it drops my audit_log table which I don't have any idea why and how it's doing so and to prevent it how to resolve. Please enlighten me on this thing.

Also if there is any other thing we can use like "update". But most importantly why is it dropping my audit_log table only.

Kramer
  • 389
  • 8
  • 34
  • See https://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – Wim Deblauwe Sep 13 '18 at 08:06
  • Are all your entity classes in the `com.package.scan` package? Also the `AuditLog` entity? – Wim Deblauwe Sep 13 '18 at 08:07
  • I have seen already but why is it dropping my audit_log what it has to do with session SessionFactory according to the above link? my question is what is really happening here – Kramer Sep 13 '18 at 08:08
  • @WimDeblauwe audit_log is com.package.scan.admin while repo is in com.package.scan.repo therefore I am scanning the base package here – Kramer Sep 13 '18 at 08:11
  • @WimDeblauwe Shouldn't I do this ? and also AuditLogRepository and UserRepository is also there in admin package while in repo only ldap repo is present – Kramer Sep 13 '18 at 08:11
  • @WimDeblauwe look at this deadly thing here https://www.onlinetutorialspoint.com/hibernate/hbm2ddl-auto-example-hibernate-xml-config.html' – Kramer Sep 13 '18 at 09:52
  • @WimDeblauwe it says If the value is create-drop then, Hibernate first checks for a table and do the necessary operations and finally drops the table after all the operations are completed. – Kramer Sep 13 '18 at 09:52

0 Answers0