找不到H2数据库的hibernate表

时间:2017-07-24 22:33:19

标签: java spring hibernate maven

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

org.h2.jdbc.JdbcSQLException: Table "DEVICE" not found

所有内容都已正确映射并与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>

Device类:

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

1 个答案:

答案 0 :(得分:2)

你错过了

<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>
  

参考Hibernate, Chapter 3. Configuration, Table 3.7. Miscellaneous Properties