java - 在数据库中使用带有 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>
解决方案
您没有连接到内存数据库。您的 JDBC URL 用于与 localhost 的网络连接:
jdbc:h2:tcp://localhost/~/test
要使用内存 H2,URL 必须如下所示,包含mem
:
jdbc:h2:mem:testdb
在手册中,请参阅In-Memory Database部分
推荐阅读
- python - python:转换 24 小时以及为什么使用 if 将返回 None
- auto-increment - 在 Gremlin 中实现行号的最简单方法
- python - 有效打印大量不同字符串列表的交集和并集
- git - 即使将我的公钥上传到 azure 也无法使用 ssh 连接到 azure devops
- reactjs - 如何在 React 中比较两个类型化对象(带接口)?
- dart - Dart 运行时类型与 'is' 关键字的比较
- visual-studio-2017 - C++/CLI 项目中的“将所有警告视为错误,除了...”
- java - Restlet 服务器 204 问题
- c# - 命名空间不能直接包含成员
- mysql - SQLSTATE[HY000] [2006] MySQL 服务器已在 docker 中消失