首页 > 解决方案 > 在数据库中使用带有 H2 的 Hibernate 时出错

问题描述

我正在使用休眠。如何配置我的applicationContext.xml以使 H2 内存数据库org.hibernate.dialect.H2Dialect不起作用

弹簧配置

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:tcp://localhost/~/test" />
    <property name="username" value="sa" />
    <property name="password " value="" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.emusicstore</value>
        </list>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

标签: javahibernateh2

解决方案


您没有连接到内存数据库。您的 JDBC URL 用于与 localhost 的网络连接:

jdbc:h2:tcp://localhost/~/test

要使用内存 H2,URL 必须如下所示,包含mem

jdbc:h2:mem:testdb

在手册中,请参阅In-Memory Database部分


推荐阅读