Hibernate does not create table when using Spring DAO

I am trying to use Spring DAO with Hibernate for a web application. When I try to save information in a DAO using

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

In Tomcat, I get the following:

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]

Log File 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 not surprising, I expect the table does not exist. A database exists, and I expect Hibernate to generate and modify tables as needed.

The hibernation display 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 Spring configuration happens 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>
+3
source share
1 answer

Try this instead:

<prop key="hibernate.hbm2ddl.auto">update</prop>
+6
source

Source: https://habr.com/ru/post/1762537/


All Articles