嵌套异常是org.hibernate.exception.SQLGrammarException:无法执行查询 - 添加了“0_”

时间:2017-03-18 13:57:07

标签: java hibernate

我是Hybernate的新手,很抱歉,如果它是愚蠢的问题。 当我运行JUnit Test时,我看到了这个错误:

could not execute query; SQL [select employee0_.id_employee as id_emplo1_5_, employee0_.date_of_birth as date_of_2_5_, employee0_.id_department as id_depar9_5_, employee0_.email as email3_5_, employee0_.home_phone as home_pho4_5_, employee0_.mobile_phone as mobile_p5_5_, employee0_.name as name6_5_, employee0_.pesel as pesel7_5_, employee0_.surname as surname8_5_ from employee employee0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query 

Hibernate在查询中添加了一些“0_”?我在mysql db中有这些列。

我的员工DAO有:

    public List<Employee> findEmployeeByDepartmentName(String name) {
        TypedQuery<Employee> query = entityManager.createQuery(
                "select e from Employee e join e.department  d where d.name = :name",
                Employee.class);
        query.setParameter("name", name);

        return query.getResultList();
    }   

和员工:

package com.capgemini.domain;

import java.io.Serializable;

//更多导入......

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long idEmployee;

    @Column(nullable = true, length = 30)
    private String name;

    @Column(nullable = true, length = 30)
    private String surname;

    @Column(nullable = false, length = 11)
    private BigInteger pesel;

    @Column(nullable = false, length = 10, columnDefinition = "DATE")
    private LocalDate dateOfBirth;

    @Column(nullable = false, length = 40)
    private String email;

    @Column(nullable = false, precision = 10)
    private BigDecimal homePhone;

    @Column(nullable = false, precision = 9)
    private BigDecimal mobilePhone;

    public Employee() {
        super();
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
//more getters....

    public void setIdEmployee(Long idEmployee) {
        this.idEmployee = idEmployee;
    }
//more setters...


}

这种侵蚀的原因是什么? 我尽可能地简化了我的问题。

3 个答案:

答案 0 :(得分:0)

不要担心Hibernate为查询添加的额外内容,这些只是它自动生成的别名(并且数字确保它们是唯一的)。

您的查询不正确,因此请在此处查找错误:

select e from Employee e join e.department  d where d.name = :name

请记住,这不是SQL,而是HQL,这是微妙的不同。

我注意到的第一件事是Employee类没有名为department的字段,所以我先检查一下。

答案 1 :(得分:0)

我好极了!我粘贴了wronge方法/代码:

@Override
public List<Employee> findEmployeeByName(String name) {
    TypedQuery<Employee> query = entityManager.createQuery(
            "SELECT e FROM Employee e where e.name like :name",
            Employee.class);
    query.setParameter("name", name);

    return query.getResultList();
}

和Eroor:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select employee0_.id_employee as id_emplo1_5_, employee0_.date_of_birth as date_of_2_5_, employee0_.id_department as id_depar9_5_, employee0_.email as email3_5_, employee0_.home_phone as home_pho4_5_, employee0_.mobile_phone as mobile_p5_5_, employee0_.name as name6_5_, employee0_.pesel as pesel7_5_, employee0_.surname as surname8_5_ from employee employee0_ where employee0_.name like ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

即使我简化它:

@Override
public List<Employee> findEmployeeByName(String name) {
    TypedQuery<Employee> query = entityManager.createQuery(
            "SELECT e FROM Employee e",
            Employee.class);
    query.setParameter("name", name);

    return query.getResultList();
}

错误是:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select employee0_.id_employee as id_emplo1_5_, employee0_.date_of_birth as date_of_2_5_, employee0_.id_department as id_depar9_5_, employee0_.email as email3_5_, employee0_.home_phone as home_pho4_5_, employee0_.mobile_phone as mobile_p5_5_, employee0_.name as name6_5_, employee0_.pesel as pesel7_5_, employee0_.surname as surname8_5_ from employee employee0_ where employee0_.name like ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

我在Workbench中执行了这个查询(删除0_),它正在运行:

use mytestdb;
select employee.id_employee as id_emplo1_5_, employee.date_of_birth as date_of_2_5_, employee.id_department as id_depar9_5_, employee.email as email3_5_, employee.home_phone as home_pho4_5_, employee.mobile_phone as mobile_p5_5_, employee.name as name6_5_, employee.pesel as pesel7_5_, employee.surname as surname8_5_ from employee employee

答案 2 :(得分:0)

我发现如果我改变了

@Column(name = "dateOfBirth", nullable = false, length = 10, columnDefinition = "DATE")
private LocalDate dateOfBirth;

@Column(name = "dateOfBirth", nullable = false, length = 10, columnDefinition = "DATE")
private Date dateOfBirth;

然后它正在工作!为什么?我该如何解决?