Search code examples
javaspringhibernatespring-data-jpahibernate-mapping

HIbernate doesn't create table that already exist in another database


I have a bean entity named com.test.Student which is mapped to a table named "student" in attendance database using xml. The "student" table in attendance database does not yet exist.

Hibernate is not creating the "student" table on attendance database on application start, even though the property hibernate.hbm2dll.auto is set to "update". It is also not generating any exception, warning or query. It simply does not do anything.

After some testing, I came to realize that a student table already exists in some other database named "sms". If I map the com.test.Student @Entity to another table name (that does not exist in any database), Hibernate will create it just fine with this config.

Why is Hibernate not creating the "student" table on the mapped database? Is the existance of another table with the same name in another database interfering in some way?

Some info:

  • Hibernate ver: 4.3.9
  • Spring ver: 4.3.9
  • MySQL as DB
  • Java 8
  • Netbeans asIDE

Solution

  • As mentioned on the comments section, if hibernate is not creating tables on ddl auto due to table name conflicts between schemas, you should explicitly declare:

    @Table(name="attendance.student")
    

    That way hibernate will create the table correcly. If no schema is explicitly declared there, it will look for other tables with the same name publicly, and therefore, wont create the new one.

    Take a look at this bug description. If you want to know more details about how hibernate behaves when creating new tables, you should definately create another post with a more concise scenario, so that we can discuss it further.