【问题标题】:Hibernate doesn't create Table - Spring MVCHibernate 不创建表 - Spring MVC
【发布时间】:2014-02-13 12:30:31
【问题描述】:

我有一个使用休眠的春季项目。当我开始项目时,数据库没有变化。

我尝试了不同的配置,但没有任何效果。

有什么想法可能是错的吗?

在 servlet.xml 中配置休眠:

<beans:beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
         xmlns:beans="http://www.springframework.org/schema/beans">

<context:component-scan base-package="com.springapp.mvc"/>

<!--Data source has the database information -->
<beans:bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="org.postgresql.Driver"/>
    <beans:property name="url" value="jdbc:postgresql://localhost:5432/Praktyki"/>
    <beans:property name="username" value="dbusersolsoft"/>
    <beans:property name="password" value="dbpassSolsoft"/>
</beans:bean>

<!-- Session Factory -->


<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="packagesToScan">
            <value>com.springapp.mvc.model</value>
    </beans:property>
    <!--<beans:property name="showSql" value="true" />-->
    <!--<beans:property name="generateDdl" value="true" />-->
    <beans:property name="hibernateProperties">

        <beans:props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">false</prop>
        </beans:props>
    </beans:property>


</beans:bean>

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

【问题讨论】:

  • 你试过&lt;prop key="hibernate.hbm2ddl.auto" value="update" /&gt;
  • @KevinBowersox 语法错误,我不能使用它

标签: java spring hibernate spring-mvc


【解决方案1】:

使用

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

根据文档,更新会更新现有架构。

link 供参考。

简而言之,

validate: validate the schema, makes no changes to the database.

update: update the schema.

create: creates the schema, destroying previous data.

create-drop: drop the schema at the end of the session.

auto : Automatically validates or exports schema DDL to the database when the SessionFactory is created

【讨论】:

    【解决方案2】:

    在开始项目之前创建一次项目并继续。大多数应用程序在启动时假定模式是先验存在的。

    【讨论】:

    • update 应该根据注释创建表。在非 spring 应用程序中,当我在 hibernate.cfg.xml 中配置所有内容时它可以工作,但现在我遇到了问题。
    • 我知道应该怎么做;不要这样做。
    【解决方案3】:

    如果您正确执行了所有步骤,但仍然出现错误,请检查“hibernate.dialect”是否设置为 MySQL5Dialect,以防您使用 MySQL 作为数据库。

    【讨论】:

    • 谢谢它的工作我不知道如何将此答案标记为无用
    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 2016-08-15
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2019-06-12
    相关资源
    最近更新 更多