Hibernate createSQLQuery给出异常 - 字符串索引超出范围:0

时间:2017-11-08 17:24:45

标签: hibernate

对于下面提到的逻辑,在行执行get exception(java.lang.StringIndexOutOfBoundsException:String index out of range:0):result = q.list();

我确信在很多地方性别都是空的。在查询时是否还有其他检查null的方法?

 beginTransaction();
        try {
            SQLQuery q = getSession().createSQLQuery("SELECT DISTINCT " +
                                                     "ind.ind_id AS id, " +
                                                     "ind.IND_FNAME AS firstName, " +
                                                     "ind.IND_LNAME AS lastName, " +
                                                     "perInfo.IND_DOB AS dob, " +
                                                     "trc.trCTM_Grade_Mapping AS grade, " +
                                                     "TIMESTAMPDIFF(YEAR, perInfo.IND_DOB, NOW()) AS years, " +
                                                     "TIMESTAMPDIFF(MONTH, perInfo.IND_DOB, NOW())  AS months, " +
                                                     "perInfo.IND_SEX AS sex " +
                                                         "FROM INDIVIDUALS ind " +
                                                             "INNER JOIN USER_MEMBERSHIPS usm on usm.USR_ID = ind.IND_ID " +
                                                             "INNER JOIN PERSONAL_INFO perInfo on perInfo.IND_ID = ind.IND_ID " +
                                                             "INNER JOIN TRACKER_CUSTOM_DASHBOARD trcd on perInfo.IND_ID = trcd.USR_ID  " +
                                                             "INNER JOIN TRACKER_CUSTOM trc on trcd.trCTM_MainID = trc.trCTM_MainID " +
                                                         "WHERE usm.GRP_ID = :GRP_ID ;");


            q.setInteger ("GRP_ID", groupId);
            q.setResultTransformer (Transformers.aliasToBean (ImmunizationsComplianceDTO.class));
            result = q.list();
        }catch(RuntimeException e) {
            rollbackTransaction();
            handleHibernateException(e);
            throw e;
        } finally {
            closeTransaction();
        }

2 个答案:

答案 0 :(得分:0)

在ImmunizationsComplianceDTO中,我确实进行了以下更改。仍然没有运气得到同样的问题...

不是简单地添加getter和setter,而是添加了性别的空检查

 if(gender != null){
                this.sex = gender;
            }

完整的吸气剂设定器如下所示。

// Getter setter for gender of the student.
    public Character getSex() {
        return sex;
    }
    public void setSex(Character gender) {
        if(gender != null){
            this.sex = gender;
        }
    }

答案 1 :(得分:0)

所以似乎性别/性别列有空值并且要克服该问题在查询中添加了以下内容

"COALESCE(perInfo.IND_SEX, '') AS sex" +

所以最终查询看起来像

beginTransaction();
        try {
            SQLQuery q = getSession().createSQLQuery("SELECT DISTINCT " +
                                                     "ind.ind_id AS id, " +
                                                     "ind.IND_FNAME AS firstName, " +
                                                     "ind.IND_LNAME AS lastName, " +
                                                     "perInfo.IND_DOB AS dob, " +
                                                     "trc.trCTM_Grade_Mapping AS grade, " +
                                                     "TIMESTAMPDIFF(YEAR, perInfo.IND_DOB, NOW()) AS years, " +
                                                     "TIMESTAMPDIFF(MONTH, perInfo.IND_DOB, NOW())  AS months, " +
                             "COALESCE(perInfo.IND_SEX, '') AS gender" + 
                                                         "FROM INDIVIDUALS ind " +
                                                             "INNER JOIN USER_MEMBERSHIPS usm on usm.USR_ID = ind.IND_ID " +
                                                             "INNER JOIN PERSONAL_INFO perInfo on perInfo.IND_ID = ind.IND_ID " +
                                                             "INNER JOIN TRACKER_CUSTOM_DASHBOARD trcd on perInfo.IND_ID = trcd.USR_ID  " +
                                                             "INNER JOIN TRACKER_CUSTOM trc on trcd.trCTM_MainID = trc.trCTM_MainID " +
                                                         "WHERE usm.GRP_ID = :GRP_ID ;");


            q.setInteger ("GRP_ID", groupId);
            q.setResultTransformer (Transformers.aliasToBean (ImmunizationsComplianceDTO.class));
            result = q.list();
        }catch(RuntimeException e) {
            rollbackTransaction();
            handleHibernateException(e);
            throw e;
        } finally {
            closeTransaction();
        }