1

I am working with Hibernate on Java with a MySQL database. If I set hbm2ddl.auto to create`, all current tables are dropped and recreated correctly, but if I set it to "update" it does nothing to the database despite reporting the following line to the console:

INFO: HHH000228: Running hbm2ddl schema update

For reference, the code I am using to set up the database is:

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure()
            .build();

While my cfg.xml file looks like:

<?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://127.0.0.1:3306/testdatabase</property>
        <property name="connection.username">xxxxxxxx</property>
        <property name="connection.password">xxxxxxxx</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>

        <!-- 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">update</property>


        <!-- Names the annotated entity class -->
        <mapping class="com.test.case.Example"/>

    </session-factory>
</hibernate-configuration>

Obviously since the create is working, I see no need to show my annotated class.

Edit:

The changes I expect to happen are basically to correct the database column types to the correct ones. Before running the update I change the column types in MySql to something different from the annotated schema but on update it's not correcting it as would be expected. In contrast when I dropped the table altogether the update did work, it would seem update is not as fully featured as expected?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Nathan Williams
  • 429
  • 6
  • 14
  • 3
    what is the result you are expecting ? update will do nothing if all tables are there and not changed, try to drop all tables and run the app with `update` i think it will create the tables. – Yazan Feb 16 '16 at 07:34
  • I am checking if the update changes the column types to the correct ones. So before running the update I change the column types in mysql to something different from the annotated schema but on update it's not correcting it as would be expected. – Nathan Williams Feb 17 '16 at 01:50
  • Just to verify I did what you said with dropping the table entirely and it worked so it seems update isn't as fully featured as expected? – Nathan Williams Feb 17 '16 at 02:16
  • 1
    i did a search about this issue, it looks hibernate will only add new columns to the db, when they are added to your entity class, check this question (look to 1st answer) http://stackoverflow.com/questions/15978368/hibernate-hbm2ddl-auto-update-doesnt-update-column-definitions-in-mysql – Yazan Feb 17 '16 at 07:35
  • Thanks, that seems to explain it. – Nathan Williams Feb 17 '16 at 11:39

1 Answers1

0

Service registry requires some properties from the configuration. And because you are using non-standard name/location of the hibernate configuration file

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
    .configure("cfg.xml")
    .build();
Roman C
  • 49,761
  • 33
  • 66
  • 176