Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 549
Nick Div Avatar asked Nov 20 '13 07:11

Nick Div


People also ask

Does hibernate create tables automatically?

Hibernate framework can be used to create tables in the database automatically. Below property is added in the configuration file to create tables automatically.

Why is hibernate not creating tables?

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.

What does Hibernate HBM to DDL Auto create mean?

0 votes. hibernate. hbm2ddl. auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.

What does Hibernate do Java?

Hibernate is an open source Object-Relational Persistence and Query service for any Java Application. Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieves the developer from most common data persistence related programming tasks.

Why can’t hibernate auto-create my table?

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.

How to create a table in hibernate?

To create a table, you need to insert below line into application.properties − Here, Hibernate will create the table demo88 automatically. The application.properties code is as follows − Now right click on the main class and click Run menu as Java Application. The application will run and the table gets created.

Does hibernate support many-to-many mapping?

It worked for me . 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.

How to create table demo88 in hibernate?

Here, Hibernate will create the table demo88 automatically. The application.properties code is as follows − Now right click on the main class and click Run menu as Java Application. The application will run and the table gets created.


4 Answers

I had the same issue, this worked for me -

<property name="hibernate.hbm2ddl.auto">create</property> 
like image 128
Sachchidanand Singh Avatar answered Sep 28 '22 10:09

Sachchidanand Singh


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.

like image 30
Kihats Avatar answered Sep 28 '22 09:09

Kihats


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.

like image 29
Carloso Avatar answered Sep 28 '22 08:09

Carloso


  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

like image 34
Dipayan Das Avatar answered Sep 28 '22 10:09

Dipayan Das