繁体   English   中英

Spring MVC JPA异常org.hibernate.exception.SQLGrammarException

[英]Spring MVC JPA exception org.hibernate.exception.SQLGrammarException

我是Spring JPA的新手。 我正在学习。 尝试在mysql数据库中插入数据时出现奇怪的错误。

我确保我的表和数据库设置与我的设置相同

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    |                |

我的实体课:-

      @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;
       }
     }

错误:-

 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

您数据库中的表名称为empolyee

@Entity
public class Employee extends AbstractPersistable<Long>

在这里,您没有指定@Table批注,因此JPA将表名作为类名,这是您的数据库中不存在的Employee。 因此改变这个

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

MySQL表名称的区分大小写取决于操作系统:

在MySQL中,数据库与data目录中的目录相对应。 数据库中的每个表都对应于数据库目录中的至少一个文件(可能还有更多文件,具体取决于存储引擎)。 因此,基础操作系统的区分大小写在数据库和表名的区分大小写中发挥了作用。 这意味着数据库和表名在Windows中不区分大小写,在大多数Unix版本中也不区分大小写。 Mac OS X是一个值得注意的例外,它基于Unix,但是使用不区分大小写的默认文件系统类型(HFS +)。 但是,Mac OS X也支持UFS卷,这与任何Unix一样都区分大小写。

因此,您需要以正确的大小写设置表名:

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

将您的实体类注释更改为以下内容。

@Entity
@Table(name="empolyee")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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