Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

java - Getting SQLSyntaxErrorException: Table doesn't exist in hibernate

I am tryin to write a very simple hibernate application. But when I am running the main method its throwing exception SQLSyntaxErrorException: Table 'testhibernate.employee' doesn't exist even though I have set hibernate.hbm2ddl.auto as "create". Details are give below. Could you please help me on this ?

Class with main method :

package com.sr.main;

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

import com.sr.entity.Employee;

public class TestHibernate {

    public static void main(String[] args) {
        // TODO Auto-generated method stubu
        Configuration config = new Configuration().configure("\hibernate.cfg.xml");
        SessionFactory factory  = config.buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        Employee emp1 = new Employee(111, "John",20000);
        session.save(emp1);
        tx.commit();
        

    }

}

Entity class :

package com.sr.entity;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
    
    @Id
    Integer empId;
    String empName;
    Integer empSal;
    
    
    public Employee(Integer empId, String empName, Integer empSal) {
        super();
        this.empId = empId;
        this.empName = empName;
        this.empSal = empSal;
    }
    public Integer getEmpId() {
        return empId;
    }
    public void setEmpId(Integer empId) {
        this.empId = empId;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public Integer getEmpSal() {
        return empSal;
    }
    public void setEmpSal(Integer empSal) {
        this.empSal = empSal;
    }
    

}

config xml :

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name = "hibernate.connection.url">
         jdbc:mysql://localhost/testHibernate
      </property>
      <property name = "hibernate.connection.username">
         root
      </property>
      <property name = "hibernate.connection.password">
         root
      </property>
      <property name="hibernate.hbm2ddl.auto">create</property>
      <property name="show_sql">true</property>
      
      <!-- List of XML mapping files -->
      <mapping class = "com.sr.entity.Employee"/>
      
   </session-factory>
</hibernate-configuration>

Exception :

java.sql.SQLSyntaxErrorException: Table 'testhibernate.employee' doesn't exist at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ... 20 more

MySql version : 5.5.50

Hibernate version : 5.4.25.Final

question from:https://stackoverflow.com/questions/65640841/getting-sqlsyntaxerrorexception-table-doesnt-exist-in-hibernate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the documentation:

The entity class must have a public or protected no-argument constructor. It may define additional constructors as well.

So, you should correct your entity in the following way:

@Entity
public class Employee {
    
   // ...    
   public Employee() {
   }

   public Employee(Integer empId, String empName, Integer empSal) {
       this.empId = empId;
       this.empName = empName;
       this.empSal = empSal;
   }
   
   // ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...