views:

72

answers:

1

I'm trying to use Spring DAO with Hibernate for a web application. When I try to persist information in the DAO using

getHibernateTemplate().save("bar", bar);

I get the following in Tomcat:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: 
[com.enw.foo.domain.Bar]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.enw.foo.domain.Bar]

The log files report:

SEVERE: Servlet.service() for servlet foo threw exception
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'foo.bar' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39

Which is no surprise, I expect that the table doesn't exist. The database does exist, and I'm counting on Hibernate to generate and alter the tables as necessary.

The hibernate-mapping file contains:

<class name="Bar" table="BAR">
  <id name="id" column="BAR_ID">
  <generator class="native"/>
    </id>
  <property name="title" column="TITLE"/>
  <property name="date" type="timestamp" column="POST_DATE"/>
</class>

And the spring configuration occurs mainly in foo-data.xml which contains:

<bean id="barDao" class="com.enw.foo.data.impl.HibernatePostDao">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>Domain.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="show_sql">true</prop>
            <prop key="hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/foo"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
    <property name="initialSize" value="5"/>
    <property name="maxActive" value="10"/>
</bean>
+2  A: 

Try this instead:

<prop key="hibernate.hbm2ddl.auto">update</prop>
Pascal Thivent
Thanks much! Could you give me a hint how you figured that out? Is there any documentation for the various properties that can be set?
FarmBoy
@Farmboy Well, I have a project using the above property which is working and it has the prefix :) Regarding the various properties, the Hibernate documentation would be the right place. See the [Chapter 3. Configuration](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html).
Pascal Thivent