在命名查询where子句中使用子查询

时间:2018-07-23 14:51:01

标签: java mysql eclipselink named-query

我有这个命名查询(简化了一些[删除了一些日期过滤器],以便一目了然):

@Entity
@XmlRootElement
@AdditionalCriteria("this.deleted <> 'deleted'")
@NamedQueries({
    @NamedQuery(name = "FacilityForm.findByDateFacility", query = "SELECT f FROM FacilityForm f WHERE f.formDate BETWEEN :from and :to AND f.id In (SELECT o.id from InventoryLog l, FormItem i, FacilityForm o WHERE (:facilityId IS NULL OR i.facilityId = :facilityId) AND l.formItemId = i.id AND i.formId = o.id) ORDER BY f.formDate DESC")
})

提取的查询字符串:

"SELECT f FROM FacilityForm f WHERE f.formDate BETWEEN :from and :to AND f.id In (SELECT o.id from InventoryLog l, FormItem i, FacilityForm o WHERE (:facilityId IS NULL OR i.facilityId = :facilityId) AND l.formItemId = i.id AND i.formId = o.id) ORDER BY f.formDate DESC"

我知道,如果我也想在一个子查询中使用主实体,则必须为其使用另一个别名,我相信我已经做到了(在子查询中使用FacilityForm o)。

我的问题是,在窗体上调用该错误时会出现此错误:

Info:   ERROR APPNAME - Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.ID)) AND (t2.FORM_ID = t1.ID.t1.ID)) AND (t3.DELETED <> 'deleted')) AND ((t2.DE' at line 1
Error Code: 1064
Call: 
SELECT t0.ID, t0.FORM_TYPE, t0.BANK_ACCOUNT_NO, t0.BANK_NAME, t0.COMMENT_BOTTOM, t0.COMMENT_UP, t0.COMPANY_ID, t0.CREATED_BY, t0.CREATED_TS, t0.CURRENCY, t0.DAILY_RATE, t0.DELETED, t0.DELIVERY_DATE, t0.DIRECTION, t0.EMAIL_SENT, t0.EU_INTERNAL_BOO, t0.FOREIGN_LANG_BOO, t0.FORM_DATE, t0.FORM_NR, t0.IBAN, t0.MODIFIED_BY, t0.MODIFIED_TS, t0.PAYMENT_BOOKED, t0.PAYMENT_DATE, t0.PAYMENT_TERM, t0.PAYMENT_TYPE, t0.PRINTED_ORIG, t0.REVERSE_CHARGE_BOO, t0.STATE, t0.STATUS, t0.SUBTYPE, t0.SWIFT, t0.TITLE, t0.TYPE, t0.USER_ID, t0.VERSION, t0.ADDRESS_DATA_ID, t0.ADDRESS_ID, t0.CONTACT_ID, t0.CONTAINER_ID_FROM, t0.CONTAINER_ID_TO, t0.FACILITY_ID_FROM, t0.FACILITY_ID_TO, t0.JOB_ID, t0.PARTNER_DATA_ID, t0.PARTNER_ID, t0.RELATED_FORM_ID, t0.WS_ORDER_ID FROM form t0 
WHERE (((((((t0.DIRECTION = ?) 
AND (t0.FORM_DATE BETWEEN ? AND ?)) AND ((? IS NULL) OR (t0.MODIFIED_TS >= ?))) AND ((? IS NULL) OR (t0.MODIFIED_TS <= ?))) AND (t0.STATUS > ?)) AND t0.ID IN (SELECT t1.ID FROM inventory_log t3, form_item t2, form t1 WHERE ((((((? IS NULL) OR (t2.FACILITY_ID = ?)) AND (t3.FORM_ITEM_ID = t2.ID.t2.ID)) AND (t2.FORM_ID = t1.ID.t1.ID)) AND (t3.DELETED <> ?)) AND ((t2.DELETED <> ?) AND ((t1.DELETED <> ?) AND (t1.FORM_TYPE = ?)))))) AND ((t0.DELETED <> ?) AND (t0.FORM_TYPE = ?))) ORDER BY t0.FORM_DATE DESC
    bind => [16 parameters bound]

相关部分:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.ID)) AND (t2.FORM_ID = t1.ID.t1.ID)) AND (t3.DELETED <> 'deleted')) AND ((t2.DE' at line 1

有人可以帮我吗,可能是什么问题?

谢谢。

0 个答案:

没有答案