如何在hibernate中编写连接查询。运行查询时获取null

时间:2013-03-23 09:44:25

标签: java hibernate

我有一个查询,当我在sqlDeveloper中运行时,我得到了结果。这是查询

select * from Losa_Cust_Reject_App rejectApp 
inner join 
    Losa_Cust losaCust 
on 
    rejectApp.app_Ref_No = losaCust.app_Ref_No 
where 
    rejectApp.app_Ref_No != '0000001912' and  rejectApp.app_Dt >= '23-SEP-2012' 
and
    rejectApp.cust_Id1 = 'A111111111' and rejectApp.cust_Id1_Type = '01'; 

我将此查询翻译为hibernate,如下所示

public List findAppByDate(String appRefNo, Date previousAppDate, String currentId1,
        String currentIdType1) {

    StringBuffer query = new StringBuffer("from ");
    query.append(LosaCustRejectApp.class.getName());
    query.append(" rejectApp inner join ");
    query.append(LosaCust.class.getName());
    query.append(" losaCust with rejectApp.appRefNo = losaCust.comp_id.appRefNo");
    query.append(" where rejectApp.appRefNo != ? and  rejectApp.appDt >= ?");
    query.append(" and rejectApp.custId1 = ? and rejectApp.custId1Type = ? ");

    List result = null;

    try {
        result = getHibernateTemplate().find(query.toString(),
                new Object[] { appRefNo, previousAppDate, currentId1, currentIdType1 });

        if (CollectionUtils.isNotEmpty(result)) {
            return result;
        }
    } catch (Exception e) {
        String message = e.getMessage();
        System.out.println();
    }
    return result;
}

但是当我运行这个查询时,它会遇到阻塞,我在catch块中得到message = null。这是我的LosaCustRejectApp

的.hbm.xml文件

LosaCustRejectApp.hbm.xml

<hibernate-mapping default-lazy="false">

<class name="com.thetasp.losa.data.LosaCustRejectApp" table="LOSA_CUST_REJECT_APP" optimistic-lock="version">

    <id name="rejectAppId" type="java.lang.Long" column="REJECT_APP_ID">
        <generator class="com.thetasp.code.runningno.HibernateId">
            <param name="fieldCode">REJECT_APP_ID</param>
            <param name="returnType">java.lang.Long</param>
        </generator>
    </id>
    <version name="rowVersion" type="int" column="ROW_VERSION" access="property" unsaved-value="undefined" />
    <property name="appRefNo" type="java.lang.String" column="APP_REF_NO" not-null="true" length="20" />
    <property name="appDt" type="java.sql.Timestamp" column="APP_DT" not-null="true" length="23" />
    <property name="custId1Type" type="java.lang.String" column="CUST_ID1_TYPE" not-null="true" length="1" />
    <property name="custId1" type="java.lang.String" column="CUST_ID1" not-null="true" length="20" />
            .....
            .....
    <property name="updateDt" type="java.sql.Timestamp" column="UPDATE_DT" length="23" />

    <!-- associations -->

</class>

这是我的LosaCust.hbm.xml

<hibernate-mapping default-lazy="false">
<class name="com.thetasp.losa.data.LosaCust" table="LOSA_CUST" discriminator-value="I" optimistic-lock="version">

    <composite-id name="comp_id" class="com.thetasp.losa.data.LosaCustPK">
        <key-property name="custId" column="CUST_ID" type="java.lang.String" length="20" />
        <key-property name="appRefNo" column="APP_REF_NO" type="java.lang.String" length="20" />
    </composite-id>

    <discriminator column="CUST_T" type="string" length="1" ></discriminator>

    <version name="rowVersion" type="int" column="ROW_VERSION" access="property" unsaved-value="undefined" />

    <property name="age" type="java.lang.Integer" column="AGE" length="10" />
    <property name="id1Type" type="java.lang.String" column="ID1_TYPE" length="1" />
    <property name="id1" type="java.lang.String" column="ID1" length="20" />
    ....
    <many-to-one name="cisCust" class="com.thetasp.cis.data.CisCust" cascade="none" update="false" insert="false" fetch="join">
        <column name="CUST_ID"></column>
    </many-to-one>

    <set name="losaCustDupAppSummaryList" lazy="true" inverse="true" cascade="delete-orphan" order-by="DUP_APP_DT DESC">
        <key>
            <column name="CUST_ID" />
            <column name="APP_REF_NO" />
        </key>
        <one-to-many class="com.thetasp.losa.data.LosaCustDupAppSummary" />
    </set>

    <set name="losaCustDebtSummaryList" lazy="true" inverse="true" cascade="delete-orphan">
        <key>
            <column name="CUST_ID" />
            <column name="APP_REF_NO" />
        </key>
        <one-to-many class="com.thetasp.losa.data.LosaCustDebtSummary" />
    </set>

    <set name="losaCustCcList" lazy="true" inverse="true" cascade="merge">
        <key>
            <column name="CUST_ID" />
            <column name="APP_REF_NO" />
        </key>
        <one-to-many class="com.thetasp.losa.data.cc.LosaCustCc" />
    </set>

    <set name="losaFinancialIncomeList" lazy="true" inverse="true" cascade="all">
        <key>
            <column name="CUST_ID" />
            <column name="APP_REF_NO" />
        </key>
        <one-to-many class="com.thetasp.losa.data.LosaFinancialIncome" />
    </set>

    <set name="losaCustUitilizationCcList" lazy="true" inverse="true" cascade="merge" >
        <key>
            <column name="CUST_ID" />
            <column name="APP_REF_NO" />
        </key>
        <one-to-many class="com.thetasp.losa.data.cc.LosaCustUtilizationCc" />
    </set>

    <set name="losaMueList" lazy="true" inverse="true" cascade="merge">
        <key>
            <column name="CUST_ID" />
            <column name="APP_REF_NO" />
        </key>
        <one-to-many class="com.thetasp.losa.data.LosaMue" />
    </set>


    <subclass name="com.thetasp.losa.data.LosaCorpCust" discriminator-value="C" lazy="false">

        <join table="LOSA_CORP_CUST" fetch="join" optional="true" >
            <key>
                <column name="CUST_ID"/>
                <column name="APP_REF_NO"/>
            </key>
            <property name="annualIncome" column="ANNUAL_INCOME" type="java.math.BigDecimal" precision="19" scale="2" />
                    .....

        </join>
    </subclass>
    <subclass name="com.thetasp.losa.data.LosaIndvCust" discriminator-value="I" lazy="false" >
        <property name="salutation" type="java.lang.String" column="SALUTATION" not-null="false" length="10" />
        <property name="midName" type="java.lang.String" column="MID_NAME" length="50" />
    ....
            ....
        <property name="verifiedTaxPayableAmt" type="java.math.BigDecimal" column="VERIFIED_TAX_PAYABLE_AMT"  precision="19"  scale="2"/>

        </subclass>
    </class>
</hibernate-mapping>

为什么我会变空?为什么这个查询没有在hibernate中运行?我做错了什么?

由于

*的 修改 -------------------------------------------------- ---------- **

我认为我因为行

而变空
query.append(" losaCust with rejectApp.appRefNo = losaCust.comp_id.appRefNo");

这是我如何调用此方法并传递值

for (LosaCust currentLosaCust : currentCustomerList) {
    String currentId1 = currentLosaCust.getId1();
    String currentIdType1 = currentLosaCust.getId1Type();
    List<LosaCustRejectApp> previousRejectedAppList = losaCustRejectAppBoc
                    .findAppByDate(appRefNo, previousAppDate, currentId1, currentIdType1);

    if (CollectionUtils.isNotEmpty(previousRejectedAppList)) {
            ....
    }

}

这是堆栈跟踪

java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:371)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3452)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3239)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3112)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
at com.thetasp.losa.dao.imp.LosaCustRejectAppDaoImp.findAppByDate(LosaCustRejectAppDaoImp.java:125)
at com.thetasp.losa.boc.imp.LosaCustRejectAppBocImp.findAppByDate(LosaCustRejectAppBocImp.java:263)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:111)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy213.findAppByDate(Unknown Source)
at com.thetasp.losa.boc.imp.LosaCustRejectAppCheckHKANZBocImp.checkForIdType1AndIdNo1(LosaCustRejectAppCheckHKANZBocImp.java:97)
at com.thetasp.losa.boc.imp.LosaCustRejectAppCheckHKANZBocImp.checkRejectApp(LosaCustRejectAppCheckHKANZBocImp.java:80)
at com.thetasp.losa.zk.composer.creditcheckresults.CreditCheckResultsHKANZComposer.onClick$btn_runcustreject(CreditCheckResultsHKANZComposer.java:219)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.zkoss.zk.ui.event.GenericEventListener.onEvent(GenericEventListener.java:81)
at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:192)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:306)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:329)
at org.zkoss.zk.ui.AbstractComponent$ForwardListener.onEvent(AbstractComponent.java:3054)
at com.thetasp.common.zk.utils.EventUtil.triggerEvent(EventUtil.java:133)
at com.thetasp.common.zk.utils.EventUtil.triggerEvent(EventUtil.java:104)
at com.thetasp.common.zk.utils.EventUtil.triggerEvent(EventUtil.java:89)
at com.thetasp.common.zk.utils.PipeEventHandler.onEvent(PipeEventHandler.java:32)
at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:192)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.impl.UiEngineImpl.processEvent(UiEngineImpl.java:1626)
at org.zkoss.zk.ui.impl.UiEngineImpl.process(UiEngineImpl.java:1410)
at org.zkoss.zk.ui.impl.UiEngineImpl.execUpdate(UiEngineImpl.java:1134)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.process(DHtmlUpdateServlet.java:562)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:457)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:465)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

0 个答案:

没有答案