简体   繁体   中英

HibernateException when trying to access embedded Derby db

I have deployed my embedded Derby database with Intellij in my project like 在此处输入图片说明

I want to access it with my Spring application, so I create a Hibernate config

@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 is location of my entity classes, and 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;
    }
}

and add this to my application.properties

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

(changing to DerbyTenFiveDialect , DerbyTenSixDialect and DerbyTenSevenDialect doesn't help) I get this exception:

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

How can I solve this?

I've changed my HibernateConfig class like

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM