SQLGrammarException

1

Good evening! Can someone help me with the error in my code, I followed the steps of the teacher but presents error in mine and not in his example. I'm using the same dependencies. Here is the snippet of the method and the error.

public List<Person> findByLastName(String lastName){
    String jpql = "from Person p where p.lastName like ?";

    return find(jpql, lastName);
}   

private static void findByLastName() {
    List<Person> persons = new PersonDAO().findByLastName("Figueira");

    for (Person person : persons) {
        System.out.println(person.toString());      
    }
}

Give this error:

  

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 Person p where p.lastName like' Figueira '' at line 1

    
asked by anonymous 20.02.2018 / 04:13

1 answer

2

Since you are using JPQL of JPA you have to set SELECT to JPQL . The HQL is who agrees to omit SELECT .

public List<Person> findByLastName(String lastName){
    String jpql = "SELECT p FROM Person p WHERE p.lastName LIKE ?";

    return find(jpql, lastName);
}   

p>     
20.02.2018 / 12:56