develop1
develop1

Reputation: 767

hibernate table not found for H2 database

I am miagrating my database from MySql to H2 and I keep getting the error message

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

Everything was mapped correctly and worked with MySql. I only changed the context.xml file to work with H2 and added a dependency for H2 in the Pom.xml file.

context.xml file:

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

The Device class:

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

Upvotes: 0

Views: 1256

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42184

You're missing

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

in

<property name="hibernateProperties">

to force Hibernate to create schema based on entity classes if it is missing. You also need to change dialect from MySQL to 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

Upvotes: 2

Related Questions