I have a spring mvc 3.1 application based on this tutorial. I’ve modified the application to an annotation based application and adapted the view handler to use InternalResourceViewResolver over jstl which is working fine.
In a nutshell the app uses spring mvc, spring data jpa and jqgrid to produce a simple user list which has basic crud mappings passing actions to the controllers through the response …
When I attempt to modify a record by an update action I’m getting 500 errors (aop trace interceptor ) …
Exception …
Servlet.service() for servlet [dispatcher] in context with path
[/spring-jqgrid-tutorial] threw exception [Request processing failed;
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.QueryParameterException:
Position beyond number of declared ordinal parameters.
Remember that ordinal parameters are 1-based!
...
at org.beckett.service.UserService.update(UserService.java:32)
UserService …
...
public Boolean update( User user ) {
User existingUser = repository.findByUsername( user.getUsername() );
if( existingUser == null ) {
return false;
}
// Only firstName, lastName, and role fields are updatable
existingUser.setFirstName( user.getFirstName() );
existingUser.setLastName( user.getLastName() );
existingUser.getRole().setRole( user.getRole().getRole() );
User saved = repository.save( existingUser );
if( saved == null )
return false; {
}
return true;
}
...
User Entity
@Entity( name = "user" )
@NamedQuery( name = User.FIND_BY_USERNAME,
query = "select u from user u where u.username = :username" )
public class User {
public static final String FIND_BY_USERNAME = "User.findByUsername";
@Id @GeneratedValue( strategy = GenerationType.AUTO )
private Long id;
private String firstName;
private String lastName;
@Column( unique = true )
private String username;
@JsonIgnore
private String password;
@OneToOne( cascade = CascadeType.ALL, mappedBy = "user" )
private Role role;
...
UserRepository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername( String username );
Page<User> findByUsernameLike( String username, Pageable pageable );
Page<User> findByFirstNameLike( String firstName, Pageable pageable );
Page<User> findByLastNameLike( String lastName, Pageable pageable );
Page<User> findByFirstNameLikeAndLastNameLike( String firstName, String lastName, Pageable pageable );
@Query( "select u from user u where u.role.role = :role" )
Page<User> findByRole( @Param( "role" ) Integer role, Pageable pageable );
}
UserController
@RequestMapping( value = "/update",
produces = "application/json",
method = RequestMethod.POST )
public @ResponseBody StatusResponse update(
@RequestParam String username,
@RequestParam String firstName,
@RequestParam String lastName,
@RequestParam Integer role ) {
User existingUser = new User( username, firstName, lastName, new Role( role ) );
Boolean result = service.update( existingUser );
return new StatusResponse( result );
}
I’ve seen several threads regarding this exception stating the requirement to change the HQL Query … However, I’m not sure how to go about debugging this issue …
This was solved by replacing the named parameter with a positional parameter for the Named Query ..