Java 创建SessionFactory时自动将架构DDL导出到数据库
我有一个应用程序(Spring4MVC+Hibernate4+MySQL+Maven集成示例,使用注释),使用基于注释的配置将Spring与Hibernate集成,我看到在启动应用程序时会自动创建模式。但是我在配置中没有这个属性hibernate.hbm2ddl.autoJava 创建SessionFactory时自动将架构DDL导出到数据库,java,mysql,hibernate,spring-mvc,jpa,Java,Mysql,Hibernate,Spring Mvc,Jpa,我有一个应用程序(Spring4MVC+Hibernate4+MySQL+Maven集成示例,使用注释),使用基于注释的配置将Spring与Hibernate集成,我看到在启动应用程序时会自动创建模式。但是我在配置中没有这个属性hibernate.hbm2ddl.auto @Configuration @EnableTransactionManagement @ComponentScan({ "fr.telecom.configuration" }) @Propert
@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;
看答案
它表示,hibernate.hbm2ddl.auto的默认行为会在创建SessionFactory时自动验证模式DDL或将其导出到数据库 看看答案
它表示,hibernate.hbm2ddl.auto的默认行为会在创建SessionFactory时自动验证模式DDL或将其导出到数据库 那么,如何避免在创建SessionFactory时自动验证或将架构DDL导出到数据库中呢?对不起,我的错误是,如果您一直阅读该答案,它会说如果您删除该属性,架构不会自动创建。无论如何,您可以发布您的hibernate配置吗?那么,如何才能避免在创建SessionFactory时自动验证或将架构DDL导出到数据库中。对不起,我的错误,如果您一直阅读该答案,它会说如果您删除该属性,架构不会自动创建。无论如何,你能发布你的hibernate配置吗?