JPQL Eclipselink - 如何连接两个表并显示另一个表中没有项目的项目

时间:2015-01-22 16:23:46

标签: java postgresql jpa eclipselink jpql

我尝试选择所有没有出价的订单,我有一个例外,我该怎么做?

这就是我想要做的事情:

SELECT o, COUNT(b) FROM OrderEntity o LEFT JOIN OrderBidEntity b ON o.id = b.orderId WHERE COUNT(b) = 0 AND o.created < :date ORDER BY o.created ASC

Exception in thread "Thread-4" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT o, COUNT(b) FROM OrderEntity o LEFT JOIN OrderBidEntity b ON o.id = b.orderId WHERE COUNT(b) = 0 AND o.created < :date ORDER BY o.created ASC]. 
[91, 99] The COUNT function is only allowed in the SELECT, GROUP BY, ORDER BY and HAVING clauses.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
    at com.sun.proxy.$Proxy38.createQuery(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce

2 个答案:

答案 0 :(得分:1)

我目前无法对此进行双重检查,但错误消息非常具有描述性。您可以尝试按以下方式调整查询:

SELECT o FROM OrderEntity o WHERE (SELECT COUNT(b) FROM OrderBidEntity b WHERE o.id = b.orderId) = 0 AND o.created < :date ORDER BY o.created ASC

答案 1 :(得分:0)

使用EMPTY检查或SIZE(而不是COUNT)检查JPQL中集合的大小。

select p from PARENT p where SIZE(p.childCollection) = 0

select p from PARENT p where p.childCollection IS EMPTY