java - Hibernate 不创建表 - Spring MVC

标签 java spring hibernate spring-mvc

我有一个使用 hibernate 的 spring 项目。当我开始项目时,数据库没有变化。

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

有什么想法是错误的吗?

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

最佳答案

使用

<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

关于java - Hibernate 不创建表 - Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21754326/

相关文章:

java - 扩展 RequestMappingHandlerMapping 以注册备用 URL 模式

java - 从迭代列表中保存对象

Java - 分阶段线程

java - java SE 应用程序中是否有图片框(使用 netbeans)

java - 线程 "main"org.openqa.selenium.WebDriverException : Returned value cannot be converted to WebElement: {ELEMENT=1} 中出现异常

java - Spring AOP 何时使用 CGLIB 代理?

java - 使用 CXF 和 Spring 在嵌入式 Jetty 中配置 JAAS

java - 在 spring 项目中使用 hibernate 工具

java - Hibernate 映射同一列两次

java - 如何?在 Java 中绘制自定义复合对象