org.hibernate.exception.SQLGrammarException:无法提取ResultSet - 从搜索查询返回匹配的记录

时间:2016-07-06 20:56:18

标签: java spring hibernate

我试图根据搜索查询从数据库中返回一条不同的记录。这是我的尝试

@SuppressWarnings("unchecked")
    @Override
    public List<Employee> getAllEmployees(String employeeName) { 
        String query = "SELECT e. FROM Employees e WHERE e.name like '%"+ employeeName +"%'";
        List<Object[]> employeeObjects = hibernateUtil.fetchAll(query);
        List<Employee> employees = new ArrayList<Employee>();
        for(Object[] employeeObject: employeeObjects) {
            Employee employee = new Employee();
            long id = ((BigInteger) employeeObject[0]).longValue();         
            int age = (int) employeeObject[1];
            String name = (String) employeeObject[2];
            float salary = (float) employeeObject[3];
            employee.setId(id);
            employee.setName(name);
            employee.setAge(age);
            employee.setSalary(salary);
            employees.add(employee);
        }
        System.out.println(employees);
        return employees;
    }

在上述尝试中,我有这个错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM Employees e WHERE e.name like '%john%'' at line 1
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

请如何修改我的查询以选择唯一记录

1 个答案:

答案 0 :(得分:0)

“SELECT e.FROM Employees e WHERE e.name like”=&gt;在SELECT e之后有一个点(。)。它应该是SELECT DISTINCT e.name FROM Employees ....