@NamedQuery将表/列名更改为大写导致exepction

时间:2016-01-01 14:23:36

标签: java mysql jpa jdbc

我在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它。

问题:

  1. 是否希望将查询转换为大写?
  2. 如何解决此问题?
  3. 我确实对此做了一些研究,但是找不到适合我情景的任何东西。有些解决方案建议更改表名(我不能这样做)或更改DAO类名,这也不是我的情况。

2 个答案:

答案 0 :(得分:4)

使用@Table注释中的反引号使JPA使用区分大小写的名称:

@Entity
@Table(name="`Course`")
@NamedQuery(name="Course.findAll", query="SELECT c FROM Course c")
public class Course implements Serializable { ...

显然,您必须使用数据库中使用的真实姓名。例如。如果名称全部为小写,请使用:

@Table(name="`course`")

答案 1 :(得分:0)

仔细阅读您所说的例外情况:

 Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table   'course_management.COURSE' doesn't exist

检查您的persistence.xml文件,即用于存储数据库的名称。

确保不要手动创建表格