2015-02-23 21 views
1

我正在使用java-play-spring模板(可在此獲取:https://github.com/jamesward/play-java-spring#master),目前我正試圖從默認的hibernate db驅動程序切換到mysql。當'hibernate.dialect'未設置時,DialectResolutionInfo不能爲null - PlayFramework

我添加一個條目,以build.sbt:

"mysql" % "mysql-connector-java" % "5.1.12" 

然後我打開application.conf和

db.default.driver=org.h2.Driver 
db.default.url="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" 

改爲:

db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost:3306/mydatabase" 

重新部署我的應用程序後,我收到一個錯誤:

Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is 
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 

我沒有改變基本配置任何東西,所以它仍然是這樣的:

@Configuration 
@EnableTransactionManagement 
public class DataConfig { 

    @Bean 
    public EntityManagerFactory entityManagerFactory() { 
     HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     vendorAdapter.setShowSql(true); 
     vendorAdapter.setGenerateDdl(true); 
     LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactory.setPackagesToScan("models"); 
     entityManagerFactory.setJpaVendorAdapter(vendorAdapter); 
     entityManagerFactory.setDataSource(dataSource()); 
     entityManagerFactory.setJpaPropertyMap(new HashMap<String, String>(){{ 
      put("hibernate.hbm2ddl.auto", "create-drop"); 
     }}); 
     entityManagerFactory.afterPropertiesSet(); 
     return entityManagerFactory.getObject(); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager() { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory()); 

     return transactionManager; 
    } 

    @Bean 
    public DataSource dataSource() { 
     final DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(Play.application().configuration().getString("db.default.driver")); 
     dataSource.setUrl(Play.application().configuration().getString("db.default.url")); 
     return dataSource; 
    } 

} 

有誰知道這可能是此錯誤的原因?下面完整的錯誤堆棧:

play.api.UnexpectedException: Unexpected exception[BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.BarService controllers.Application.barService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'barServiceImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class configs.DataConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set] 
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:166) ~[play_2.10-2.3.7.jar:2.3.7] 
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:130) ~[play_2.10-2.3.7.jar:2.3.7] 
    at scala.Option.map(Option.scala:145) ~[scala-library.jar:na] 
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:130) ~[play_2.10-2.3.7.jar:2.3.7] 
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:128) ~[play_2.10-2.3.7.jar:2.3.7] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.BarService controllers.Application.barService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'barServiceImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class configs.DataConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.BarService controllers.Application.barService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'barServiceImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class configs.DataConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'barServiceImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class configs.DataConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:356) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class configs.DataConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:601) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1008) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory configs.DataConfig.entityManagerFactory()] threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver$3.run(ConstructorResolver.java:584) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_31] 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final] 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final] 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:205) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final] 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final] 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final] 

回答

1

由我自己解決了這個問題,我只是修改數據源配置如下圖所示:

@Bean 
public DataSource dataSource() { 
    final DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName(Play.application().configuration().getString("db.default.driver")); 
    dataSource.setUrl(Play.application().configuration().getString("db.default.url")); 
    dataSource.setUsername(Play.application().configuration().getString("db.default.user")); 
    dataSource.setPassword(Play.application().configuration().getString("db.default.password")); 
    return dataSource; 
} 

是不是終究難,留下以備將來播放newbs。

相關問題