【问题标题】:enforce spring boot configuration to use hibernate/mysql instead of orientdb强制 Spring Boot 配置使用 hibernate/mysql 而不是 orientdb
【发布时间】: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


【解决方案1】:

我认为你会发现它很有用

http://docs.spring.io/spring-boot/docs/1.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#howto-two-datasources.

它展示了如何定义多个数据源并将其中一个分配为主。

这是一个相当完整的示例,还包含分发事务 - 如果您需要的话。

http://fabiomaffioletti.me/blog/2014/04/15/distributed-transactions-multiple-databases-spring-boot-spring-data-jpa-atomikos/

您需要创建 2 个配置类,分离模型/存储库包等以简化配置。

此外,在上面的示例中,它手动创建数据源。您可以使用 spring doc 上的方法和 @ConfigurationProperties 注释来避免这种情况。这是一个例子:

http://xantorohara.blogspot.com.tr/2013/11/spring-boot-jdbc-with-multiple.html

希望这些有所帮助。

【讨论】:

    【解决方案2】:

    这是构建休眠属性的正确方法

    【讨论】:

    • edit您的帖子并将实际内容显示为文本而不是屏幕截图。其他人无法从您的图像中复制和粘贴。 See here 了解详情。谢谢。
    猜你喜欢
    • 2017-04-08
    • 2020-03-16
    • 2014-06-21
    • 2017-02-10
    • 2018-06-02
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多