为什么不休眠创建我的表?

时间:2014-12-23 00:34:53

标签: java mysql xml hibernate hbm

尝试通过hibernate创建表时,我发现了这个错误。 如有必要,我可以包含相关文件。可能的原因是什么,我该如何解决它?

INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table if exists MOVIES
Hibernate: create table MOVIES (MOVIE_ID varchar(255) not null auto_increment, title varchar(255), genres varchar(255), primary key (MOVIE_ID))
Dec 23, 2014 12:27:07 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table MOVIES (MOVIE_ID varchar(255) not null auto_increment, title varchar(255), genres varchar(255), primary key (MOVIE_ID))

hibernate.cfg.xml中

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/testingbeta</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>


        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="Movie.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

Movie.hbm.xml

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

<hibernate-mapping package="org.beans">

    <class name="Movie" table="MOVIES">
        <id name="movieId" column="MOVIE_ID">
            <generator class="native"/>
        </id>
        <property name="title"/>
        <property name="genres"/>
    </class>

</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

我想这就是问题所在:

MOVIE_ID varchar(255) not null auto_increment

基于此,我猜movieId有一个String数据类型。例如,将其更改为Integer。您不能将Hibernate生成器使用的auto_incrementvarchar数据类型一起使用。