[英]@NamedQuery changes table/column name to upper case causing exepction
我在MySql中有一個名為Course的表,在Course.java類中有以下內容
package pckt.book.ch7.jpa;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the Course database table.
*
*/
@Entity
@NamedQuery(name="Course.findAll", query="SELECT c FROM Course c")
public class Course implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private int credits;
private String name;
@Column(name="Teacher_id")
private int teacher_id;
public Course() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public int getCredits() {
return this.credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getTeacher_id() {
return this.teacher_id;
}
public void setTeacher_id(int teacher_id) {
this.teacher_id = teacher_id;
}
}
我正在另一個類中按以下方式運行查詢
@PersistenceContext
EntityManager entityManager;
public List<Course> getCourseEntities(){
//Use named query created in Course entitiy
//using @NamedQuery annotation
TypedQuery<Course> courseQuery = entityManager.createNamedQuery("Course.findAll",Course.class);
return courseQuery.getResultList();
}
我收到以下錯誤:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'course_management.COURSE' doesn't exist
Error Code: 1146
Call: SELECT ID, CREDITS, NAME, Teacher_id FROM COURSE
Query: ReadAllQuery(name="Course.findAll" referenceClass=Course sql="SELECT ID, CREDITS, NAME, Teacher_id FROM COURSE")
從讀取錯誤開始,聽起來問題在於@NamedQuery生成的查詢是大寫的,因此找不到COURSE表。
在my.cnf中,我有lower_case_table_names = 2,但是不確定是否應該有所作為。
我的persistance.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="CourseManagementEJB">
<jta-data-source>jdbc/CourseManagement</jta-data-source>
<class>pckt.book.ch7.jpa.Course</class>
</persistence-unit>
</persistence>
jdbc / CourseManagement是GlassFish中的JNDI資源,它指向Course_management數據庫的連接池,其中Course是一個表。 我知道我可以成功連接到數據庫,因為我能夠ping通它。
問題:
我對此進行了一些研究,但找不到適合我的情況的任何東西。 一些解決方案建議更改表名(我不能這樣做)或更改DAO類名,這也不是我的情況。
在@Table
批注中使用反引號使JPA用例區分大小寫:
@Entity
@Table(name="`Course`")
@NamedQuery(name="Course.findAll", query="SELECT c FROM Course c")
public class Course implements Serializable { ...
顯然,您必須使用數據庫中使用的真實名稱。 例如,如果名稱全為小寫,請使用:
@Table(name="`course`")
通讀您的例外情況:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'course_management.COURSE' doesn't exist
檢查您的persistence.xml文件(用於存儲數據庫的名稱)。
確保您沒有手動創建表格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.