当我尝试执行createSQLQuery()时出现此异常引起:java.sql.SQLException:列名无效

时间:2014-08-13 08:57:37

标签: hibernate

hql = "SELECT USER_CODE, USER_NAME, USER_SHORT_NAME, PRINT_NAME, USER_PASSWORD, REMARKS, RECORD_ACTIVE,"+
                        " INACTIVE_WEF, CREATED_BY, CREATED_ON, MODIFIED_BY, MODIFIED_ON " +
                        "(SELECT DESGN_NAME FROM MCDESIGNATIONMASTER WHERE DESGN_CODE = :DESIGNATION_CODE)TSUSERMASTER_DESIGNATION " +
                        " FROM MCUSERMASTER WHERE USER_CODE LIKE NVL(:USER_CODE,'%') AND USER_NAME LIKE NVL(:USER_NAME,'%')";
                System.out.println("hql----"+hql);
                Query query = session.createSQLQuery(hql).addEntity(UserMasterModel.class).setParameter("USER_CODE",roleMasterModel.getUserCode()).setParameter("USER_NAME", roleMasterModel.getUserName());

1 个答案:

答案 0 :(得分:0)

  1. 您在MODIFIED_ON之后错过了一个逗号,但如果没有SQL代码缩进,很难看到它:

  2. 您不能将列选择与查询结果混合使用。如果第二个查询返回多行,该怎么办?

  3. 您只使用两个参数USER_CODE和USER_NAME,但您的第二个子选择会再添加一个(DESIGNATION_CODE)并且您永远不会提供其值。

  4. 由于这两个查询之间没有关联,我认为这是你必须拥有的:

    Query mcMasterQuery = session
        .createSQLQuery(
        "SELECT "+
        "   USER_CODE, "+
        "   USER_NAME, "+
        "   USER_SHORT_NAME, "+
        "   PRINT_NAME, "+
        "   USER_PASSWORD, "+
        "   REMARKS, "+
        "   RECORD_ACTIVE, "+
        "   INACTIVE_WEF, "+
        "   CREATED_BY, "+
        "   CREATED_ON, "+
        "   MODIFIED_BY, "+
        "   MODIFIED_ON "+
        "FROM MCUSERMASTER "+
        "WHERE "+
        "   USER_CODE LIKE NVL(:USER_CODE,'%') AND "+
        "   USER_NAME LIKE NVL(:USER_NAME,'%')"
        )
        .addEntity(UserMasterModel.class)
        .setParameter("USER_CODE", roleMasterModel.getUserCode())
        .setParameter("USER_NAME", roleMasterModel.getUserName());
    
    Query mcDesignationQuery = session
        .createSQLQuery(
        "SELECT " +
        "   DESGN_NAME " +
        "FROM MCDESIGNATIONMASTER " +
        "WHERE " +
        "   DESGN_CODE = :DESIGNATION_CODE" 
        )
        .addEntity(UserMasterModel.class)
        .setParameter("DESIGNATION_CODE", designationCode);