尝试访问嵌入式Derby数据库时出现HibernateException

时间:2016-01-27 08:52:02

标签: java spring hibernate derby

我已经在我的项目中使用Intellij部署了我的嵌入式Derby数据库 enter image description here

我想用Spring应用程序访问它,所以我创建了一个Hibernate配置

@Configuration
@EnableTransactionManagement
public class HibernateConfig {

    @Autowired
    @Bean
    public SessionFactory getSessionFactory(final DataSource dataSource) {
        final LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.scanPackages("com.em.daas.entity");
        return sessionBuilder.buildSessionFactory();
    }
}

wher com.em.daas是我的实体类的位置,以及Datasource config:

@Configuration
@PropertySource(TestApplication.PROPERTIES_PATH)
public class LocalDataSourceConfig {

    @Bean
    public DriverManagerDataSource derbyDataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
        dataSource.setUrl("jdbc:derby:C:/Users/timofb/Documents/diploma/daas/src/test/resources/db/MetadataStorage");
        dataSource.setUsername("");
        dataSource.setPassword("123");
        return dataSource;
    }
}

并将其添加到我的application.properties

spring.jpa.database-platform=org.hibernate.dialect.DerbyDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

(更改为 DerbyTenFiveDialect DerbyTenSixDialect DerbyTenSevenDialect 没有帮助) 我得到了这个例外:

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at com.emc.daas.config.HibernateConfig.getSessionFactory(HibernateConfig.java:24)
at com.emc.daas.config.HibernateConfig$$EnhancerBySpringCGLIB$$f623976c.CGLIB$getSessionFactory$0(<generated>)
at com.emc.daas.config.HibernateConfig$$EnhancerBySpringCGLIB$$f623976c$$FastClassBySpringCGLIB$$8ece3a4e.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
at com.emc.daas.config.HibernateConfig$$EnhancerBySpringCGLIB$$f623976c.getSessionFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 36 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我改变了我的HibernateConfig类,如

public SessionFactory getSessionFactory(final DataSource dataSource) {
    final LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
    sessionBuilder.addProperties(properties);
    sessionBuilder.scanPackages("com.em.daas.entity");
    return sessionBuilder.buildSessionFactory();
}