我有一个基于本教程的 spring mvc 3.1 应用程序。我已将应用程序修改为基于注释的应用程序,并调整视图处理程序以在 jstl 上使用 InternalResourceViewResolver,这工作正常。
简而言之,该应用程序使用 spring mvc、spring data jpa 和 jqgrid 来生成一个简单的用户列表,该列表具有基本的 crud 映射,通过响应将操作传递给控制器......
当我尝试通过更新操作修改记录时,我收到 500 个错误(aop 跟踪拦截器)...
例外...
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)
用户服务...
...
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;
}
...
用户实体
@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;
...
用户存储库
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 );
}
用户控制器
@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 );
}
我已经看到了几个关于这个异常的线程,说明了更改 HQL 查询的要求......但是,我不确定如何调试这个问题......