Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring启动,Hibernate启动两次

我在使用Hibernate和Spring Boot时遇到问题。 Hibernate加载两次,所以它会创建数据库,然后再次创建它(使用hbm2ddl.auto=create,所以我希望是一次)

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = {
        UserMapper.class,
        EventMapper.class,
        GroupMapper.class,

        UserServiceImpl.class,
        EventServiceImpl.class,
        GroupServiceImpl.class,

        UserController.class
})
@PropertySource(value = {"classpath:application.properties"})
@Import({ DatabaseConfig.class })
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

和数据库配置:

@EnableJpaRepositories(basePackageClasses = {
        EventRepository.class,
        GroupRepository.class,
        UserRepository.class
})
@EnableTransactionManagement
public class DatabaseConfig {
    private static final String DB_DRIVER_CLASS = "db.driver";
    private static final String DB_PASSWORD = "db.password";
    private static final String DB_URL = "db.url";
    private static final String DB_USERNAME = "db.username";
    private static final String DB_SSL = "db.ssl";
    private static final String DB_SSL_FACTORY = "db.ssl.factory";
    private static final String DB_FORMAT_SQL = "db.formatSql";
    private static final String DB_DIALECT = "db.dialect";
    private static final String DB_SHOW_SQL = "db.show_sql";
    private static final String DB_HBM2DLL_AUTO = "db.hbm2ddl.auto";
    private static final String DB_POOL_SIZE = "db.poolSize";

    @Resource
    private Environment env;

    @Bean
    public SessionFactory sessionFactory() throws IOException {
        final LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
        factory.setDataSource(dataSource());
        factory.setPackagesToScan(packagesToScan());
        factory.setHibernateProperties(dataSourceProperties());
        factory.setAnnotatedClasses(classesToAdd());
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    private DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getRequiredProperty(DB_DRIVER_CLASS));
        dataSource.setUrl(env.getRequiredProperty(DB_URL));
        dataSource.setUsername(env.getRequiredProperty(DB_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(DB_PASSWORD));
        dataSource.setConnectionProperties(connectionPropertiesDev());
        return dataSource;
    }

    public Class[] classesToAdd() {
        return new Class[]{

        };
    }

    public String[] packagesToScan() {
        return new String[] {""};
    }

    private Properties dataSourceProperties() {
        Properties props = new Properties();
        props.put("hibernate.connection.driver_class", env.getRequiredProperty(DB_DRIVER_CLASS));
        props.put("hibernate.connection.url", env.getRequiredProperty(DB_URL));
        props.put("hibernate.connection.username", env.getRequiredProperty(DB_USERNAME));
        props.put("hibernate.connection.password", env.getRequiredProperty(DB_PASSWORD));
        props.put("hibernate.connection.pool_size", env.getRequiredProperty(DB_POOL_SIZE));
        props.put("hibernate.connection.requireSSL", env.getRequiredProperty(DB_SSL));
        props.put("hibernate.hbm2ddl.auto", env.getRequiredProperty(DB_HBM2DLL_AUTO));
        props.put("hibernate.show_sql", env.getRequiredProperty(DB_SHOW_SQL));
        props.put("hibernate.dialect", env.getRequiredProperty(DB_DIALECT));
        props.put("hibernate.format_sql", env.getRequiredProperty(DB_FORMAT_SQL));
        return props;
    }

    private Properties connectionPropertiesDev() {
        Properties properties = new Properties();
        properties.put("ssl", env.getRequiredProperty(DB_SSL, Boolean.class));
        properties.put("sslfactory", env.getRequiredProperty(DB_SSL_FACTORY));
        return new Properties();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(packagesToScan());
        entityManagerFactoryBean.setJpaProperties(dataSourceProperties());
        return entityManagerFactoryBean;
    }

    @Bean
    public HibernateTransactionManager transactionManager() throws IOException {
        final HibernateTransactionManager txManager = new HibernateTransactionManager(sessionFactory());
        txManager.afterPropertiesSet();
        return txManager;
    }

}

有人知道为什么会这样吗


共 (1) 个答案