首页 > 解决方案 > Spring JPA Hibernate 使用 create-drop 自动删除表

问题描述

我在DBConfiguration文件中配置Criteria API ,我有一个表,命名为我在其中记录登录用户的日期时间、状态和用户名。audit_log

这是我的 DBConfiguration 文件设置

@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;
       }

}

现在,当我使用 create-drop 时,它会删除我的 audit_log 表,我不知道它为什么以及如何这样做,并阻止它如何解决。请赐教这件事。

此外,如果还有其他我们可以使用的东西,例如"update"。但最重要的是为什么它只删除我的 audit_log 表。

标签: postgresqlspring-data-jpacriteria-api

解决方案


推荐阅读