javamysqlxmlhibernatehbm

Why wont hibernate create this my table?


I'm getting this error out when attempting to create a table through hibernate. I can include the relevant files if necessary. What is likely the cause, and how can I go about fixing it?

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>

Solution

  • I guess this is the problem:

    MOVIE_ID varchar(255) not null auto_increment
    

    Based on this I guess that movieId has a String datatype. Change it to Integer for instance. You can't use auto_increment which is used by the Hibernate generator with varchar datatype.