【问题标题】:Automatically exports schema DDL to the database when the SessionFactory is created创建 SessionFactory 时自动将 schema DDL 导出到数据库
【发布时间】:2015-12-16 17:00:07
【问题描述】:

我有一个应用程序(使用注释的 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;

【问题讨论】:

    标签: java mysql hibernate spring-mvc jpa


    【解决方案1】:

    this的回答。

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

    【讨论】:

    • 在创建 SessionFactory 时如何避免自动验证或导出模式 DDL 到数据库。
    • 对不起,我的错误,如果您一直阅读该答案,它会说如果您删除该属性,则不会自动创建架构。无论如何,您可以发布您的休眠配置吗?
    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2015-05-02
    • 2012-02-07
    相关资源
    最近更新 更多