QueryException:JPA样式的位置参数不是整数序号

时间:2019-05-26 05:37:03

标签: java sql hibernate spring-boot

我收到“ QueryException:JPA样式的位置参数不是整数序号” 我尝试通过其他解决方式来解决,例如org.hibernate.QueryException: JPA-style positional param was not an integral ordinal,但仍然无法正常工作。

我尝试将?1形式的硬编码更改为数据库中已有的一些数据,并且可以工作,但是更改为?1后,它将再次出现错误。

---硬编码之前(有错误) -在存储库中-

@Query(
    value = "SELECT user_att_workshop.id, user_att_workshop.user_id, user_att_workshop.workshop_att_id, skill.skill_name, user_att_workshop.skill_score " + 
            "FROM user_att_workshop LEFT JOIN skill on user_att_workshop.skill_id = skill.id " + 
            "WHERE user_att_workshop.user_id = ?1 " + 
            "AND user_att_workshop.workshop_att_id = ?2;", 
            nativeQuery = true
)
public List<UserScoreByWorkshopAtt> findUserScoreByUserIdAndWorkshopId(int userId, int workshopId);

-服务中-

public List<UserScoreByWorkshopAtt> findUserScoreByUserIdAndWorkshopIdtest(
    int userId, 
    int workshopId) {

    return 
        userScoreByWorkshopAttRepository.findUserScoreByUserIdAndWorkshopId(
        userId,
        workshopId
    );
}

-在控制器中-

List<UserScoreByWorkshopAtt> getUserScooreByUserId(
    @PathVariable("user_id") int userId, 
    @PathVariable("workshop_id") int workshopId) {

    return userScoreByWorkshopAttService.findUserScoreByUserIdAndWorkshopIdtest(
        userId, 
        workshopId
    );

}

---硬编码后(可以运行并且没有错误) -在存储库中-

 @Query(value = "SELECT user_att_workshop.id, user_att_workshop.user_id, user_att_workshop.workshop_att_id, skill.skill_name, user_att_workshop.skill_score FROM user_att_workshop LEFT JOIN skill on user_att_workshop.skill_id = skill.id  WHERE user_att_workshop.user_id = 5 AND user_att_workshop.workshop_att_id = 2;", nativeQuery = true)
public List<UserScoreByWorkshopAtt> findUserScoreByUserIdAndWorkshopId();

-服务中-

public List<UserScoreByWorkshopAtt> findUserScoreByUserIdAndWorkshopIdtest() {
    return userScoreByWorkshopAttRepository.findUserScoreByUserIdAndWorkshopId();
}

-在控制器中-

List<UserScoreByWorkshopAtt> getUserScooreByUserId(@PathVariable("user_id") int userId,
            @PathVariable("workshop_id") int workshopId) {
        return userScoreByWorkshopAttService.findUserScoreByUserIdAndWorkshopIdtest();
    }
  

由于:org.hibernate.QueryException:JPA样式的位置参数   在...上不是整数   org.hibernate.engine.query.spi.ParameterParser.parse(ParameterParser.java:195)     在   org.hibernate.engine.query.spi.ParamLocationRecognizer.parseLocations(ParamLocationRecognizer.java:56)     在   org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.getParameterMetadata(NativeQueryInterpreterStandardImpl.java:30)     在   org.hibernate.engine.query.spi.QueryPlanCache.getSQLParameterMetadata(QueryPlanCache.java:128)     在   org.hibernate.internal.AbstractSharedSessionContract.getNativeQueryImplementor(AbstractSharedSessionContract.java:1000)     ...还有85个

1 个答案:

答案 0 :(得分:0)

您可以这样写吗:

@Query(
    value = "SELECT user_att_workshop.id, user_att_workshop.user_id, user_att_workshop.workshop_att_id, skill.skill_name, user_att_workshop.skill_score " + 
            "FROM user_att_workshop LEFT JOIN skill on user_att_workshop.skill_id = skill.id " + 
            "WHERE user_att_workshop.user_id = :userId " + 
            "AND user_att_workshop.workshop_att_id = :workshopId;", 
            nativeQuery = true
)
public List<UserScoreByWorkshopAtt> findUserScoreByUserIdAndWorkshopId(@Param("userId")int userId,@Param("workshopId") int workshopId);

或从查询末尾删除';'并检查是否有空格

@Query(
    value = "SELECT user_att_workshop.id, user_att_workshop.user_id, user_att_workshop.workshop_att_id, skill.skill_name, user_att_workshop.skill_score " + 
            "FROM user_att_workshop LEFT JOIN skill on user_att_workshop.skill_id = skill.id " + 
            "WHERE user_att_workshop.user_id =?1 " + 
            "AND user_att_workshop.workshop_att_id =?2", 
            nativeQuery = true
)
public List<UserScoreByWorkshopAtt> findUserScoreByUserIdAndWorkshopId(int userId, int workshopId);