JPA查询:JOIN问题

时间:2013-09-23 09:54:17

标签: jpa jpql

我正在进行JPA查询:

<named-query name="AUDIT_QUERY">
    <query>
select rx.rxNumber, rx.rxFillStatus.id, tx.rx.rxaaId, tx.id, tx.drugUpc.id, 
tx.compound.id, tx.tpSpecialServiceFee.id,tx.txHistoryDrugUpc.id, 
tx.txHistoryCompound.id, tx.txHistoryTpSpecialServiceFee.id, 
tx.txHistoryRx.id, tx.txNumber,tx.txStatus.id,tx.txAdjudicationStatus.id, 
tx.serviceDate, tx.createDatetime, tx.txQuantity, tx.remainingQuantity, tx.daysSupply, 
tx.ppTotal, tx.tpPaidTotal, tx.adjustedTotal, sig.sigDescriptionStoreLanguage , 
sig.sigDescriptionPatientLanguage , tx.serviceDate
from 
Rx rx JOIN FETCH Tx tx JOIN TxSig sig WHERE rx.rxaaId = tx.rx.rxaaId
and tx.txSig.id = sig.id WHERE 
rx.patient.id = ? 
order by tx.serviceDate desc, tx.txNumber desc,
tx.id desc
    </query>
</named-query>

解析时,hibernate抛出错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: WHERE near line 1, column 620 [select rx.rxNumber, rx.rxFillStatus.id, tx.rx.rxaaId, tx.id, tx.drugUpc.id, tx.compound.id, tx.tpSpecialServiceFee.id,tx.txHistoryDrugUpc.id, tx.txHistoryCompound.id, tx.txHistoryTpSpecialServiceFee.id, tx.txHistoryRx.id, tx.txNumber,tx.txStatus.id,tx.txAdjudicationStatus.id, tx.serviceDate, tx.createDatetime, tx.txQuantity, tx.remainingQuantity, tx.daysSupply, tx.ppTotal, tx.tpPaidTotal, tx.adjustedTotal, sig.sigDescriptionStoreLanguage , sig.sigDescriptionPatientLanguage , tx.serviceDate from com.sdm.hw.rx.dao.entity.Rx rx JOIN FETCH Tx tx JOIN TxSig sig WHERE rx.rxaaId = tx.rx.rxaaId and tx.txSig.id = sig.id WHERE rx.patient.id = ? order by tx.serviceDate desc, tx.txNumber desc, tx.id desc]

任何帮助将不胜感激。

谢谢, Giriraj

1 个答案:

答案 0 :(得分:0)

要定义加入条件,请使用关键字ON代替WHERE。如果我看对了你就错了。

SELECT
    rx.rxNumber,
    rx.rxFillStatus.id,
    tx.rx.rxaaId,
    tx.id,
    tx.drugUpc.id,
    tx.compound.id,
    tx.tpSpecialServiceFee.id,
    tx.txHistoryDrugUpc.id,
    tx.txHistoryCompound.id,
    tx.txHistoryTpSpecialServiceFee.id,
    tx.txHistoryRx.id,
    tx.txNumber,
    tx.txStatus.id,
    tx.txAdjudicationStatus.id,
    tx.serviceDate,
    tx.createDatetime,
    tx.txQuantity,
    tx.remainingQuantity,
    tx.daysSupply,
    tx.ppTotal,
    tx.tpPaidTotal,
    tx.adjustedTotal,
    sig.sigDescriptionStoreLanguage,
    sig.sigDescriptionPatientLanguage,
    tx.serviceDate
FROM
    Rx rx
JOIN
FETCH
    Tx tx
JOIN
    TxSig sig
ON
    rx.rxaaId = tx.rx.rxaaId
AND tx.txSig.id = sig.id
WHERE
    rx.patient.id = ?
ORDER BY
    tx.serviceDate DESC,
    tx.txNumber DESC,
    tx.id DESC