1

The Situation Ok, so I am learning Hibernate(from 2 days so pardon any blunder), I am using mysql database and just trying tocreate a table Employee and make an entry in database(A demonstration).

Here are the codes

POJO:

package com.sopra.pojo;
import javax.persistence.*;
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
    @Id
    private int Id;
    private String firstName;
    private String lastName;
    private int salary;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

}

hibernate.cfg.xml which is in src

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name = "hibernate.hbm2ddl.auto">create</property>

        <mapping class="com.sopra.pojo.Employee" />
    </session-factory>
</hibernate-configuration>

EmployeeDBManager.java

package com.sopra.pojo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class EmployeeDBManager {
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setFirstName("Salim");
        e.setId(672);
        e.setLastName("Shamim");
        e.setSalary(266);
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session =  sessionFactory.openSession();
        Transaction tx =  session.beginTransaction();

        session.persist(e);

        tx.commit();

        session.close();
    }
}

The Error

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist

I've tried

  1. use this in cfg file : <property name="hibernate.hbm2ddl.auto">update</property>
  2. What is interesting is when I manually create the table in mysql using workbench, hibernate does drops it as when i use drop command it says the table doesn't exists.

I can't figure out where the glitch might be(newbie and internet didn't helped). Please mention any additional details required in comments. Please help! Thanks.

Salim Shamim
  • 656
  • 10
  • 25

3 Answers3

2

I made it worked, I am not sure if it's a proper answer but since it got it working I am posting it as an answer. I made two changes

  1. I was using hibernate version 5.2.12 so I changed it to 4.2.0 and added jars from two more folders which earlier I hadn't (In summary I have now imported Jars from :optional, provided & required folder in hibernate user library)
  2. I overlooked a url mistake which was

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    

In the dtd url I have not added www. It should have been

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">`

So, my mistake.

Anyway, comments regarding improving this answer and why it made it work are deeply appreciated.

halfer
  • 19,824
  • 17
  • 99
  • 186
Salim Shamim
  • 656
  • 10
  • 25
0

add different auto option of hibernate,

Use instead of create , update or create-drop

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

or

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

You can read more about it here

Daniel Taub
  • 5,133
  • 7
  • 42
  • 72
0
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

You have not written mysql version in dialect property

Correction:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

Hope this helps to you cause it certainly did for me.

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81