Java Hibernate 不创建表 - Spring MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21754326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:30:12  来源:igfitidea点击:

Hibernate doesn't create Table - Spring MVC

javaspringhibernatespring-mvc

提问by Mr Jedi

I have spring project where I'm using hibernate. When I start project there is no change in db.

我有一个使用休眠的 spring 项目。当我开始项目时,db 没有变化。

I try difrenf configurations but nothings worked.

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

Any ideas what can be wrong?

任何想法可能是错误的?

Hibernate is configure in servlet.xml:

Hibernate 在 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" />

采纳答案by VinayVeluri

use

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

As per the documentation, update updates the existing schema.

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

linkfor reference.

链接供参考。

In brief,

简单来说,

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

回答by duffymo

Create the project once before you start the project and move on. Most applications assume the schema exists a priori when they start.

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

回答by Sriram

In case if you have followed all the procedures correctly, but still getting the error, check whether the "hibernate.dialect" is set to MySQL5Dialect in case you use the MySQL as the database.

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