代码之家  ›  专栏  ›  技术社区  ›  Eric Wilson

使用Spring DAO时Hibernate不创建表

  •  3
  • Eric Wilson  · 技术社区  · 16 年前

    我正在尝试将springdao与Hibernate一起用于web应用程序。当我尝试使用

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

    我在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]
    

    日志文件报告:

    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
    

    hibernate映射文件包含:

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

    弹簧配置主要发生在 foo-data.xml

    <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>
    
    1 回复  |  直到 16 年前
        1
  •  6
  •   Pascal Thivent    16 年前

    请尝试以下操作:

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