haizhenhua1680
2012-06-26 14:50 阅读 229
已采纳

hibernate查询的问题

最近在做项目的过程中,遇到了一个让我觉得很困扰的问题,到这里来问一下各位!

 

  public String doQuery2() throws Exception {
        
        contractReviewInfo.setPopedom(getPopedom());
        
        if(!"1".equals(contractReviewInfo.getReadOnly())){
            contractReviewInfo.setErrorMessage(Message.MES_NO_POPEDOM);
            return "recordList2";
        }
        
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
        BaseDao baseDao = (BaseDao) ctx.getBean("baseDao");
        
        contractReviewInfo.setContractClass(baseDao.getLovMappingValue("contract_class_contract"));
        
        String hql = "select a from ArchiveContractDraft a,ReviewInfo r where a.archiveContractCode = r.archiveContractNo and a.contractType = '2' and r.examineResults = '0' and r.reviewClass = '1'";
        
        String queryKey1 = contractReviewInfo.getQueryKey1();
        String queryKey2 = contractReviewInfo.getQueryKey2();
        
        if(queryKey1 != null && !"".equals(queryKey1)){
            hql = hql + " and a.contractDraftName like '%" + queryKey1 + "%'";
        }
        if(queryKey2 != null && !"".equals(queryKey2)){
            hql = hql + " and a.isUrgent = '" + queryKey2 + "'";
        }
        
        List<ArchiveContractDraft> draftlist = baseDao.find(hql);
        
        PaginationSupport paginationSupport = new PaginationSupport(draftlist,draftlist.size()/20,20,0);
        contractReviewInfo.setPaginationSupport(paginationSupport);
        contractReviewInfo.setStartIndex(0);
        
        return "recordList2";
    }

 以上代码,如果queryKey都为空的话,可以正常查出相应信息,如果选择了queryKey2,也可正常查出,但是当选择了queryKey1时,在plsql中可以查出的数据在程序中却查不出.不知道问题出在了那里,请大家帮忙看一下吧.下面我贴一下语句中涉及到的表的映射文件内容.

 

<?xml version = '1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.yzwb.cctv.domain.ReviewInfo" table="REVIEW_INFO" schema="CCTV">
        <id name="ID" column="ID" type="string">
            <generator class="sequence">
                <param name="sequence">REVIEW_INFO_SEQ</param>
            </generator>
        </id>
        <property name="submissionDate" column="SUBMISSION_DATE" type="date"></property>
        <property name="censorsCertificate" column="CENSORS_CERTIFICATE" type="string"></property>
        <property name="reviewTimes" column="REVIEW_TIMES" type="integer"></property>
        <property name="accraditationDate" column="ACCRADITATION_DATE" type="date"></property>
        <property name="examineResults" column="EXAMINE_RESULTS" type="string"></property>
        <property name="alterReason" column="ALTER_REASON" type="string"></property>
        <property name="firstExaminer" column="FIRST_EXAMINER" type="string"></property>
        <property name="secondExaminer" column="SECOND_EXAMINER" type="string"></property>
        <property name="comments" column="COMMENTS" type="string"></property>
        <property name="reviewClass" column="REVIEW_CLASS" type="string"></property>
        <property name="archiveAgreementNo" column="ARCHIVE_AGREEMENT_NO" type="string"></property>
        <property name="archiveContractNo" column="ARCHIVE_CONTRACT_NO" type="string"></property>
        <property name="creatDate" column="CREAT_DATE" type="date"></property>
        <property name="creatPerson" column="CREAT_PERSON" type="string"></property>
        <property name="giveUpReason" column="GIVEUP_REASON" type="string"></property>
    </class>
</hibernate-mapping>

 以上为ReviewInfo

 

<?xml version = '1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yzwb.cctv.domain.ArchiveContractDraft" table="ARCHIVE_CONTRACT_DRAFT" schema="cctv">
    <id name="archiveContractCode" column="ARCHIVE_CONTRACT_CODE" type="string">
        <generator class="assigned"></generator>
    </id>
    <property name="contractDraftName" column="CONTRACT_DRAFT_NAME" type="string"/>
    <property name="archiveContractClass" column="ARCHIVE_CONTRACT_CLASS" type="string"/>
    <property name="isUrgent" column="IS_URGENT" type="string"/>
    <property name="createDate" column="CREATE_DATE" type="date"/>
    <property name="createPerson" column="CREATE_PERSON" type="string"/>
    <property name="createDepartment" column="CREATE_DEPARTMENT" type="string"/>
    <property name="auditStatus" column="AUDIT_STATUS" type="string"/>
    <property name="auditPerson" column="AUDIT_PERSON" type="string"/>
    <property name="auditDate" column="AUDIT_DATE" type="date"/>
    <property name="submissionStatus" column="SUBMISSION_STATUS" type="string"/>
    <property name="filingStatus" column="FILING_STATUS" type="string"/>
    <property name="contractType" column="CONTRACT_STATUS" type="string"/>
</class>
</hibernate-mapping>

 以上为ArchiveContractDraft

PS:我已经把映射文件配置在applicationContext文件中了,其他地方该配置的我也配置好了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    iteye_1328 iteye_1328 2012-06-26 22:34

    1、检查是否queryKey1 前后是否有空格,
    2、查看数据库是否只有一条记录是否满足queryKey1的查询条件,如果是的话,检查分页是否对了
    3、是否乱码,查看queryKey1的字符编码 打印出SQL debug的时候,copy生成的SQL放数据库里面执行

    PS:写个简单的单元测试吧,直接run下

    点赞 评论 复制链接分享
  • guazixing guazixing 2012-06-26 15:33

    感觉说的有些乱,不知道是不是你这里写的有问题:
    [code="java"]
    if(queryKey1 != null && !"".equals(queryKey1)){

    hql = hql + " and a.contractDraftName like '%" + queryKey1 + "%'";

    }

    if(queryKey2 != null && !"".equals(queryKey2)){

    hql = hql + " and a.isUrgent = '" + queryKey2 + "'";

    }

    [/code]

    queryKey1和queryKey2都不为空的话,不知道你的组成的条件是不是你想要的。
    不过有一个办法,肯定可以找到问题:把sql打印出来,对比hibernate生成的sql和你写的sql的查询条件有什么不同

    点赞 评论 复制链接分享
  • zyn010101 zyn010101 2012-06-27 08:01

    检查contractReviewInfo的queryKey1是否带空格。

    点赞 评论 复制链接分享
  • mayufenga1 helloword321 2012-06-27 08:47

    写的太乱,你自己调试下

    点赞 评论 复制链接分享

相关推荐