0

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 changed the column types in MySQL to something different from the annotated schema, but on update, Hibernate is not correcting them as it would be expected. In contrast, when I dropped the table altogether, the update did work. It would seem the update is not fully featured as expected?

5
  • 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. Commented Feb 16, 2016 at 7: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. Commented Feb 17, 2016 at 1: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? Commented Feb 17, 2016 at 2: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) stackoverflow.com/questions/15978368/… Commented Feb 17, 2016 at 7:35
  • Thanks, that seems to explain it. Commented Feb 17, 2016 at 11:39

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.