gpt4 book ai didi

java - 未找到 H2 数据库的 hibernate 表

转载 作者:行者123 更新时间:2023-12-02 02:42:07 25 4
gpt4 key购买 nike

我正在将数据库从 MySql 迁移到 H2,并且不断收到错误消息

org.h2.jdbc.JdbcSQLException:找不到表“DEVICE”

一切都已正确映射并可与 MySql 配合使用。我仅更改了 context.xml 文件以使用 H2,并在 Pom.xml 文件中添加了 H2 的依赖项。

context.xml 文件:

<mvc:annotation-driven /> 

<mvc:resources mapping="/resources/**" location="/resources/" />


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/dataStore2"/>
<property name="username" value="" />
<property name="password" value="" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

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

设备类:

package com.entities;
@Entity
@Table(name="DEVICE")
public class Device {
...
}

最佳答案

你失踪了

<prop key="hibernate.hbm2ddl.auto">create</prop>

<property name="hibernateProperties">

强制 Hibernate 根据实体类创建模式(如果缺少)。您还需要将方言从 MySQL 更改为 H2:

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

Reference: Hibernate, Chapter 3. Configuration, Table 3.7. Miscellaneous Properties

关于java - 未找到 H2 数据库的 hibernate 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45291115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com