简体   繁体   中英

Spring MVC JPA exception org.hibernate.exception.SQLGrammarException

I'm new to Spring JPA stuff. I'm learning now. I got weird error while trying to insert data in mysql database.

I made sure that I have a the table and database setup correctly as same as my

mysql> show columns in empolyee in test;


| Field | Type        | Null | Key | Default | Extra          |

| id    | int(6)      | NO   | PRI | NULL    | auto_increment | 
  eid   | int(6)      | YES  |     | NULL    |                |
| name  | varchar(40) | NO   |     | NULL    |                |
| role  | varchar(20) | YES  |     | NULL    |                |

My entity class:-

      @Entity
     public class Employee extends AbstractPersistable<Long> {

    private int eid;
    private String name;
    private String role;

    public Employee(){

    }

    public Employee(int aeid, String aname, String arole){
        eid=aeid;
        name = aname;
        role = arole;
       }
     }

Error:-

 HTTP Status 500 - Request processing failed; nested exception is  org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: Table 'test.employee' doesn't exist; 
nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 
Table 'test.employee' doesn't exist

Your table name in database is empolyee

@Entity
public class Employee extends AbstractPersistable<Long>

Here you are not specifying @Table annotation hence JPA will take table name as class name which is Employee which doesn't exist in your database. Hence change this

@Entity
@Table(name="empolyee")  //this exists in database
public class Employee extends AbstractPersistable<Long>

Case sensitivity of MySQL table names depends on operating system:

In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix. One notable exception is Mac OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive. However, Mac OS X also supports UFS volumes, which are case sensitive just as on any Unix.

So, you need to set table name in the correct case :

 @Entity @Table(name = "empolyee")
 public class Employee extends AbstractPersistable<Long>  { ... }

Change your entity class annotation to below.

@Entity
@Table(name="empolyee")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM