Hibernate不会自动创建表 [英] Hibernate does not create tables automatically

查看:92
本文介绍了Hibernate不会自动创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Hibernate和Spring框架的Maven项目。
我希望Hibernate自动创建表,但所有现有的表只是被删除,并且不会创建所需的表。
在会话工厂初始化期间不会抛出任何异常,但是当我尝试保存 Player 实体时,抛出异常:


com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'billboarddb.player'不存在

如果我手动创建表并将属性 hibernate.hbm2ddl.auto 更改为validate,那么一切正常。
你有什么想法,为什么Hibernate不创建表?

Spring配置文件:

 < context:component-scan base-package =org.meluk.billboard.business.controller/> 
< tx:annotation-driven transaction-manager =txManager/>
< bean id =propertyConfigurerclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =locations>
< list>
<值> /WEB-INF/config/jdbc.properties< /值>
< / list>
< / property>
< / bean>
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =$ {hibernate.connection.driver_class}/>
< property name =urlvalue =$ {hibernate.connection.url}/>
< property name =usernamevalue =$ {hibernate.connection.username}/>
< property name =passwordvalue =$ {hibernate.connection.password}/>
< / bean>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocationvalue =/ WEB-INF / hibernate.cfg.xml/>
< property name =packagesToScanvalue =org.meluk.billboard.jpa/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {hibernate.dialect}< / prop>
< prop key =hibernate.show_sql> $ {hibernate.show_sql}< / prop>
< prop key =hibernate.hbm2ddl.auto> $ {hibernate.hbm2ddl.auto}< / prop>
< prop key =hibernate.c3p0.min_size> $ {hibernate.c3p0.min_size}< / prop>
< prop key =hibernate.c3p0.max_size> $ {hibernate.c3p0.max_size}< / prop>
< prop key =hibernate.c3p0.timeout> $ {hibernate.c3p0.timeout}< / prop>
< prop key =hibernate.c3p0.max_statements> $ {hibernate.c3p0.max_statements}< / prop>
< /道具>
< / property>
< / bean>

< bean id =txManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

jdbc.properties文件:

  hibernate.connection.driver_class = com.mysql.jdbc.Driver 
hibernate.connection.url = jdbc:mysql://127.0.0.1:3306 / BillboardDB
hibernate.connection.username = root
hibernate.connection.password = 1234
hibernate.default_schema = BillboardDB
hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto =创建
hibernate.show_sql = true
hibernate.c3p0.min_size = 5
hibernate.c3p0.max_size = 20
hibernate.c3p0.timeout = 1800
hibernate.c3p0.max_statements = 50

Hibernate依赖关系:

 < dependency> 
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> $ {hibernateVersion}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-c3p0< / artifactId>
< version> $ {hibernateVersion}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-tools< / artifactId>
< version> $ {hibernateToolsVersion}< / version>
< /依赖关系>


解决方案

我解决了这个问题。 Hibernate无法使用MysqlInnoDBDialect创建表。
现在我改用MySQL5InnoDBDialect。


I have Maven project with Hibernate and Spring framework. I want Hibernate to create tables automatically, but all existing tables are just dropped and the required tables are not created. No exceptions are thrown during session factory initialization, but when I try save a Player entity, exception is thrown:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'billboarddb.player' doesn't exist

If I create the tables manually and change the property hibernate.hbm2ddl.auto to "validate", then everything works fine. Do you have any idea, why Hibernate does not create the tables?

Spring configuration file:

<context:component-scan  base-package="org.meluk.billboard.business.controller" />
<tx:annotation-driven transaction-manager="txManager" />
<bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" >
        <list>
            <value>/WEB-INF/config/jdbc.properties</value>
        </list>
    </property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${hibernate.connection.driver_class}" />
    <property name="url" value="${hibernate.connection.url}" />
    <property name="username" value="${hibernate.connection.username}" />
    <property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
    <property name="packagesToScan" value="org.meluk.billboard.jpa" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
            <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
            <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
            <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
        </props>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

jdbc.properties file:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/BillboardDB
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.default_schema=BillboardDB
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=create
hibernate.show_sql=true
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50

Hibernate dependencies:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>${hibernateVersion}</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
  <version>${hibernateVersion}</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-tools</artifactId>
  <version>${hibernateToolsVersion}</version>
</dependency>

解决方案

I solve the problem. Hibernate could not create tables with MysqlInnoDBDialect. Now I use MySQL5InnoDBDialect instead.

这篇关于Hibernate不会自动创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆