java - 创建 SessionFactory 时自动将模式 DDL 导出到数据库

标签 java mysql hibernate spring-mvc jpa

我有一个应用程序(使用注释的 Spring 4 MVC+Hibernate 4+MySQL+Maven 集成示例),使用基于注释的配置将 Spring 与 Hibernate 集成,我看到当我启动应用程序时自动创建了模式。但我在配置中没有此属性hibernate.hbm2ddl.auto!

  @Configuration
    @EnableTransactionManagement
    @ComponentScan({ "fr.telecom.configuration" })
    @PropertySource(value = { "classpath:application.properties" })
    public class HibernateConfiguration {

        @Autowired
        private Environment environment;

        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
           LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
           em.setDataSource(dataSource());
           em.setPackagesToScan(new String[] { "fr.telecom.model" });

           JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
           em.setJpaVendorAdapter(vendorAdapter);
           em.setJpaProperties(hibernateProperties());

           return em;
        }


        @Bean
        public LocalSessionFactoryBean sessionFactory() {
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            sessionFactory.setDataSource(dataSource());
            sessionFactory.setPackagesToScan(new String[] { "fr.telecom.model" });
            sessionFactory.setHibernateProperties(hibernateProperties());
            return sessionFactory;
         }

        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
            dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
            dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
            dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
            return dataSource;
        }

        private Properties hibernateProperties() {
            Properties properties = new Properties();
            properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
            properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
            properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
            return properties;        
        }

        @Bean
        @Autowired
        public HibernateTransactionManager transactionManager(SessionFactory s) {
           HibernateTransactionManager txManager = new HibernateTransactionManager();
           txManager.setSessionFactory(s);
           return txManager;
        }
    }

创建的表

CREATE TABLE `t_device` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `device_key` varchar(50) DEFAULT NULL,
  `device_type` varchar(50) DEFAULT NULL,
  `device_desc` varchar(100) DEFAULT NULL,
  `application_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_l3xyqw1dscspkcf3xhn4xiw8` (`device_key`),
  KEY `application_id` (`application_id`),
  CONSTRAINT `FK_pgmvl7toup3p7yem734512uf` FOREIGN KEY (`application_id`) REFERENCES `t_application` (`id`),
  CONSTRAINT `t_device_ibfk_1` FOREIGN KEY (`application_id`) REFERENCES `t_application` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

最佳答案

this回答。

它表示 hibernate.hbm2ddl.auto 的默认行为会在创建 SessionFactory 时自动验证或将模式 DDL 导出到数据库。

关于java - 创建 SessionFactory 时自动将模式 DDL 导出到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317828/

相关文章:

java - 在 JavaFX 中调整 ToggleButton 的大小

java - wsgen 使用 @WebMethod 生成 WSDL 但忽略 @WebParam

mysql - 正则表达式:仅匹配单个字符(MySQL)?

java - 如何在不更新旧记录的情况下使用 Hibernate session.save() 插入/保存对象

java - 使用 c3p0 在 Hibernate 中进行客户端堆栈跟踪

java - Hibernate完整性约束冲突: NOT NULL check constraint: For onetoOne Mapping using spring boot crud

java - 如何在 selenium webdriver 3.0 beta 中使用 geckodriver?

java - 用于测试的 gradle 项目的惯用结构

php - 有没有办法在使用 PHP 发布到 SQL 数据库之前复制数据?

基于总和的 hibernate 属性