JPQL query with WHERE on nested fields

后端 未结 1 1239
Happy的楠姐
Happy的楠姐 2021-01-02 09:51

I have a java entity class UserBean with a list of events:

@OneToMany
private List events;

EventBean has Date variable:

相关标签:
1条回答
  • 2021-01-02 10:01

    Path u.events.eventDate is an illegal construct in JPQL, because it is not allowed to navigate via a collection valued path expression. In this case u.events is a collection valued path expression. In JPA 2.0 specification this is told with following words:

    It is syntactically illegal to compose a path expression from a path expression that evaluates to a collection. For example, if o designates Order, the path expression o.lineItems.product is illegal since navigation to lineItems results in a collection. This case should produce an error when the query string is verified. To handle such a navigation, an identification variable must be declared in the FROM clause to range over the elements of the lineItems collection.

    This problem can be solved by using JOIN:

    SELECT distinct(u) 
    FROM UserBean u JOIN u.events e 
    WHERE u.name = :someName
          AND e.eventDate > :startDate 
          AND e.eventDate < :endDate
    
    0 讨论(0)
提交回复
热议问题