Hibernate is not auto-creating a table that does not exist in the DB
Asked Answered
R

14

24

I have a basic Hibernate code, I have set the property "hibernate.hbm2ddl.auto" as update still it is not auto-creating the table in the Database.

These are the required files:

employee.hbm.xml

<hibernate-mapping>
  <class name="contacts.employee" table="contacts">
      <meta attribute="class-description"></meta>
    <id column="contactId" name="contactId" type="string">
      <generator class="assigned"/>
    </id>
    <property column="contactName" length="100" name="contactName" not-null="true" type="string"/>
    <property column="password" length="100" name="password" not-null="true" type="string"/>
    <set cascade="all" name="groupOfResponsibilities" table="employee_responsibilty">
      <key column="contactId"/>
      <many-to-many class="contacts.responsibilities" column="responsibilityId"/>

    </set>

  </class>
</hibernate-mapping>

responsibility.hbm.xml

<hibernate-mapping>
  <class name="contacts.responsibilities" table="responsibilities">
    <meta attribute="class-description">
        This class list of responsibilities if an employee
    </meta>
    <id column="responsibilityId" name="responsibilityId" type="long">
      <generator class="increment"/>
    </id>
    <property column="responsibilityName" name="responsibilityName" type="string"/>
  </class>
</hibernate-mapping>

hibernate.cfg.xml

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/****</property>
    <property name="hibernate.connection.username">*****</property>
    <property name="hibernate.connection.password">*****</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.show_sql">true</property>
    <mapping resource="contacts/employee.hbm.xml"/>
    <mapping resource="contacts/responsibilitiy.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

This is the Main.java that I am trying to run:

public class Main {

    public static void main(String[] args) {

        SessionFactory sessionfactory = NewHibernateUtil.getSessionFactory();
        Transaction transaction = null;
        try {
            Session session = sessionfactory.openSession();
            transaction = session.beginTransaction();
            Set<responsibilities> groups = new HashSet<responsibilities>();
            responsibilities responsibilityOne=new responsibilities("Java");
            responsibilities responsibilityTwo=new responsibilities("SQL");
            responsibilities responsibilityThree=new responsibilities("Oracle");
            groups.add(responsibilityOne);
            groups.add(responsibilityTwo);
            groups.add(responsibilityThree);
            String uuid = UUID.randomUUID().toString();
            String uuid2 = UUID.randomUUID().toString();
            employee firstEmployee;
            firstEmployee = new employee(uuid, "Mike", groups);
            employee secondEmployee = new employee(uuid2, "Marc", groups);
            session.save(responsibilityOne);
            session.save(responsibilityTwo);
            session.save(responsibilityThree);
            session.save(firstEmployee);
            session.save(secondEmployee);

            transaction.commit();
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {

        }

    }
}

This is the error that I get:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table '**.responsibilities' doesn't exist

Rossiya answered 20/11, 2013 at 7:2 Comment(0)
B
26

I had the same issue, this worked for me -

<property name="hibernate.hbm2ddl.auto">create</property>
Blooming answered 23/2, 2015 at 9:58 Comment(2)
Also to me. Likely nobody on the Earth can say, why the $§%&"§$ do we have to use sometimes "hibernate.something" property and sometimes only "something"...Pichardo
I'm facing the same issue and i've tried create before also but it didn't work. I dont know whether I'll have to open a new question.Strongminded
V
24

The following scenario could be another reason why Hibernate cannot auto-create your table:

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private String empID;
  private String name;
}   

The table Employee will not be auto-created by Hibernate since empID is a String and it has the @GeneratedValue annotation. empID should be an int or long. I had this problem sometime and I changed my id field that had the @GeneratedValue annotation to be of type int and Hibernate auto-created the table.

Vale answered 11/10, 2015 at 14:49 Comment(0)
H
12

I had the same problem, I solved it by changing:

org.hibernate.dialect.MySQLInnoDBDialect

to

org.hibernate.dialect.MySQLDialect

I found the solution in this question.

Hauberk answered 6/10, 2014 at 13:30 Comment(2)
Same goes for the spatial versions of the dialects. Hibernate has a lot of mysterious error logs.Barnyard
The referred question suggests: MySQL5InnoDBDialect instead of MySQLInnoDBDialect which worked for me.Unequal
G
4
  1. First, check your MySQL version with $mysql --version $mysql Ver 14.14 Distrib 5.5.11

Then change Dialect accordingly

  1. then change the dialect property /For this example I have given MySQL55Dialect/ org.hibernate.dialect.MySQL55Dialect

//////////////////////////////////////////////// --where you can find Dialect:

org.hibernate.dialect
try 
import org.hibernate.dialect.ctrl+space
you find option

Hope this Help

Grew answered 11/7, 2018 at 9:55 Comment(0)
S
3

I had the same issue, but for me the solution was:

<property name="hibernate.hbm2ddl.auto">create-drop</property>

instead of

<property name="hibernate.hbm2ddl">create-drop</property>
Scrupulous answered 23/10, 2014 at 8:27 Comment(0)
A
2

Why don't you try create or create-drop instead. Seems to serve your purpose. http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional

Amblyopia answered 10/2, 2014 at 12:36 Comment(0)
L
1

I add to Spring application.properties and work

spring.datasource.driver.class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
Listless answered 27/12, 2020 at 10:3 Comment(0)
M
0

If u are Using Hibernate 5 or above this will be solved by changing the dialect to org.hibernate.dialect.MySQL5Dialect. It worked for me .

Myer answered 8/10, 2020 at 17:43 Comment(0)
R
0

for me, there was space in table name in hibernate annotation. Once removed space, table got created.

Raddy answered 1/9, 2022 at 20:31 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Linehan
H
-1

Hibernate can create table, hibernate sequence and tables used for many-to-many mapping on your behalf but you have to explicitly configure it by calling setProperty("hibernate.hbm2ddl.auto", "create") of Configuration object. By Default it just validate the schema with DB and fail if anything not already exist by giving error "ORA-00942: table or view does not exist".

Hotblooded answered 12/11, 2017 at 16:30 Comment(0)
P
-1

For the existing working environment, or for any big application area, it's not recommended to set hibernate.hbm2ddl.autoon create or create-drop , as it is shown below

<property name="hibernate.hbm2ddl.auto">create-drop</property>

though it works, but it will drop all the existing tables of the database every time you restart the server.

If you are using the hibernate jar version 5.2.0 then please switch to either lower or any other version.This version jar is having some issue regarding update.

It worked for me and i hope it will work for others too.

Poignant answered 9/8, 2018 at 14:21 Comment(0)
D
-1

after 3 hours In my case i was using wrong groupID and artifact in the packages names.

Downpipe answered 20/6, 2021 at 6:44 Comment(0)
C
-1

Adding this in application.properties works for me

spring.jpa.generate-ddl=true
Chelicera answered 30/7, 2021 at 12:23 Comment(0)
D
-2

Just do correct configuration

1) Create, Every time a new table will create. All Old data will be deleted.

<property name="hibernate.hbm2ddl.auto">create</property>

2) Update, If any modification has done then same will reflect to DB.

<property name="hibernate.hbm2ddl.auto">update</property>
Drucilla answered 10/8, 2018 at 7:59 Comment(1)
This is not answering the reason for failure. I guess everyone knows what option create or update do. This is a very generic answer, without understanding the question.Ovarian

© 2022 - 2025 — McMap. All rights reserved.