简体   繁体   中英

JPQL Query for ElementCollection

Assuming I have the following Entities and relationship:

@Entity
@Table(name = "CUSTOMER")
public class Customer {
    @Id
    private long id;

    @ElementCollection
    @CollectionTable( name="ORDER", joinColumns=@JoinColumn(name="ORDER_ID") )  
    private List<Order> orders;

}

@Embeddable
public class Order {

    private long price;

}

Now I want to get all customers who don't have any orders or their orders price are below $100. I tried to it in many ways but it always generate something like:
SELECT t0.ID FROM CUSTOMER t0, ORDERS t1 WHERE ((t1.PRICE > 100) AND (t0.COACH_ID = t1.ID))

The problem is that it return all customers who have orders above 100 but not the ones who didn't make any order yet. It is caused because of the t0.COACH_ID = t1.ID.

I am using eclipse link 2.5 and I tried all options but couldn't make it.

Any help will be appreciated.

Have you tried something like this:

SELECT DISTINCT c FROM Customer c 
LEFT JOIN c.orders o
WHERE o.price < 100 
OR c.orders IS EMPTY

I believe LEFT JOIN will do an outer-join, so it should still include customers without orders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM