3
public List<Data> List(String name, String id, int lowerBound, int upperBound) throws WiMatchException {
    try {
        Session session = getHibernateTemplate().getSessionFactory().openSession();
        SQLQuery query = session.createSQLQuery("select * from data inner join route on (data.id=route.data_id and data.status=:status  and data.is_active='Y' and route.owner_id= :ownerId) LIMIT lowerBound,upperBound ");
        query.addEntity(UserData.class);
        query.setString("status", status);
        query.setString("ownerId", ownerId);
        query.setInteger("lowerBound", lowerBound);
        query.setInteger("upperBound", upperBound);
        List<UserData> resList = query.list();

        logger.debug("size of List() =" + resList.size());
        session.close();
        return resList;

    } catch (Exception e) {
                 ...
    }
}

执行上述方法时,出现以下异常

java.lang.IllegalArgumentException: Parameter lowerBound does not exist as a named parameter in [select * from data inner join route on (data.id=route.data_id and data.status=:status  and data.is_active='Y' and route.owner_id= :ownerId) LIMIT lowerBound,upperBound ]

需要建议来解决这个问题

4

1 回答 1

7

lowerBound您在和前面缺少参数指示符(冒号)upperBound

SQLQuery query = session.createSQLQuery(
    "select * from data "+
    "inner join route on (data.id=route.data_id and data.status=:status  and data.is_active='Y' and route.owner_id= :ownerId)"+
    "LIMIT :lowerBound,:upperBound "
);

如果没有这些指示符,JDBC 不知道这些是参数,从而将它们与列名混淆。

于 2012-05-25T03:49:36.220 回答