Hibernate 4不创建表

Posted

技术标签:

【中文标题】Hibernate 4不创建表【英文标题】:Hibernate 4 not creating Tables 【发布时间】:2012-05-09 04:12:41 【问题描述】:

我正在使用 mysql5,我的 applicationContext.xml 有以下 bean:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/ems" />
    <property name="username" value="root" />
    <property name="password" value="admin" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hbm2ddl.auto">create</prop>
            <prop key="connection.url">jdbc:mysql://localhost:3306/ems</prop>
            <prop key="connection.username">root</prop>
            <prop key="connection.password">admin</prop>
            <prop key="connection.driver_class">com.mysql.jdbc.Driver</prop>
        </props>
    </property>
    <property name="mappingDirectoryLocations" value="/WEB-INF/resources/mappings" />
</bean>

我使用 MySQL Workbench 创建了一个名为“ems”的模式,但是当我在控制台中部署应用程序时,它会打印:

01:22:54,823 INFO  [org.hibernate.cfg.Environment] (MSC service thread 1-2) HHH000206: hibernate.properties not found
01:22:54,827 INFO  [org.hibernate.cfg.Environment] (MSC service thread 1-2) HHH000021: Bytecode provider name : javassist
01:22:54,841 INFO  [org.hibernate.cfg.Configuration] (MSC service thread 1-2) HHH000220: Reading mappings from file: C:\Development\JBoss-AS-7.1.0.Final\standalone\tmp\vfs\tempf3cbb10675b49730\EMSApplication.war-43fc183fcacfc03f\WEB-INF\resources\mappings\User.hbm.xml
01:22:56,884 INFO  [org.hibernate.dialect.Dialect] (MSC service thread 1-2) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
01:22:56,892 INFO  [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (MSC service thread 1-2) HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
01:22:56,901 INFO  [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (MSC service thread 1-2) HHH000399: Using default transaction strategy (direct JDBC transactions)
01:22:56,908 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (MSC service thread 1-2) HHH000397: Using ASTQueryTranslatorFactory

控制台也不例外。但是在模式 em 中,表是空的,这意味着没有创建表。

User.hbm.xml 驻留在指定位置,即:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.jboss.org/dtd/hibernate/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="app.dev.ems.data.model.User" table="USER">
        <id name="id" column="ID">
            <generator class="native"/>         
        </id>
    </class>
</hibernate-mapping>

User.java 也存在。

我正在使用 hibernate-core-4.1.1.Final 和 spring-orm-3.1.1.RELEASE。

我做错了什么?

【问题讨论】:

【参考方案1】:

我犯了一个错误: &lt;prop key="hbm2ddl.auto"&gt;create&lt;/prop&gt; 应该是 &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;

【讨论】:

有什么区别? @Fakher 如果我没记错的话,这是根据文档。

以上是关于Hibernate 4不创建表的主要内容,如果未能解决你的问题,请参考以下文章

Play + JPA + Hibernate + PostgreSQL:无法创建表

hibernate动态创建表

Hibernate创建表不成功,sql语法错误

无法从实体类 Hibernate 4.2.7 Websphere 8.5.5 oracle 11g 创建表

Spring Boot + Hibernate + Postgres - 不创建表

运行服务器时,Hibernate 不创建表