【发布时间】:2015-02-25 00:45:51
【问题描述】:
我正在使用一个使用 spring boot 的 maven 模块,它同时具有 hibernate/mysql 来保存一些数据。在另一个模块中(应该在第一个模块的依赖列表中),我们使用 orientdb 作为持久化数据库。
当我删除第二个模块时,第一个模块使用休眠并且一切正常。虽然,当我将第二个模块添加到第一个模块的依赖项时,spring boot 尝试使用 orientdb 进行持久性,这不是预期的操作,我们希望它使用 hibernate。
如何强制 Spring Boot 使用休眠模式?
Application.java
@Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); CrawlerFactory.main(null); } }
Configuration.java
@Configuration
@EnableTransactionManagement
public class DatabaseConfig {
// ==============
// PRIVATE FIELDS
// ==============
@Autowired
private Environment _env;
@Autowired
private DataSource _dataSource;
@Autowired
private LocalContainerEntityManagerFactoryBean _entityManagerFactory;
// ==============
// PUBLIC METHODS
// ==============
/**
* DataSource definition for database connection. Settings are read from
* the application.properties file (using the _env object).
*/
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(_env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(_env.getProperty("spring.datasource.url"));
dataSource.setUsername(_env.getProperty("spring.datasource.username"));
dataSource.setPassword(_env.getProperty("spring.datasource.password"));
return dataSource;
}
/**
* Declare the JPA entity manager factory.
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(_dataSource);
// Classpath scanning of @Component, @Service, etc annotated class
entityManagerFactory.setPackagesToScan(
_env.getProperty("entitymanager.packagesToScan"));
// Vendor adapter
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
// Hibernate properties
Properties additionalProperties = new Properties();
additionalProperties.put(
"hibernate.dialect",
_env.getProperty("hibernate.dialect"));
additionalProperties.put(
"hibernate.show_sql",
_env.getProperty("hibernate.show_sql"));
additionalProperties.put(
"hibernate.hbm2ddl.auto",
_env.getProperty("hibernate.hbm2ddl.auto"));
entityManagerFactory.setJpaProperties(additionalProperties);
return entityManagerFactory;
}
/**
* Declare the transaction manager.
*/
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager =
new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
_entityManagerFactory.getObject());
return transactionManager;
}
/**
* PersistenceExceptionTranslationPostProcessor is a bean post processor
* which adds an advisor to any bean annotated with Repository so that any
* platform-specific exceptions are caught and then rethrown as one
* Spring's unchecked data access exceptions (i.e. a subclass of
* DataAccessException).
*/
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
application.properties
# Database
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spices_crawler
spring.datasource.username=root
spring.datasource.password=1458
# Hibernate
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
entitymanager.packagesToScan=com.mta.spicegraph.builder
【问题讨论】:
-
那么两个模块共享一个包(坏主意)?
标签: spring spring-data spring-boot spring-data-jpa