![](/img/trans.png)
[英]JPA Exception: 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.