I need to update the values into update native query from EmployeeDTO class and I have used following implemetation. But I got this error in console, even though the syntax is correct for query:
Space is not allowed after parameter prefix ':'
Entity class:
@NamedNativeQuery( name = "Employee.updateEmployeeRecord",
query = "update employee set first_name = :#{#emp.firstName}, last_name = :#{#emp.lastName}, " +
"address = :#{#emp.address} where id = :#{#emp.id}"
)
@Entity
public class Employee {
//codes
}
DTO class:
public class EmployeeDTO {
private int id;
private String firstName;
private String lastName;
private String address;
// getter and setters
}
Repository class:
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query(name = "Employee.updateEmployeeRecord", nativeQuery = true)
void updateEmployeeRecord(@Params("emp") EmployeeDTO empDto);
}
Service Implementation class - update statement:
employeeRepository.updateEmployeeRecord(empDto);
Why it is throwing error even the syntax and query are correct? How can I solve it?
As I tried, The Spring Expression Language(SpEL) does not support for @NamedNativeQuery. So I wrote the same query within @Query annotation in the repository method and I removed the NamedNativeQuery. As I expected It worked for me!
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query(value = "update employee set first_name = :#{#emp.firstName}, last_name = :#{#emp.lastName}, address = :#{#emp.address} where id = :#{#emp.id}", nativeQuery = true)
void updateEmployeeRecord(@Params("emp") EmployeeDTO empDto);
}