gpt4 book ai didi

jpa - 在嵌套字段上使用WHERE进行JPQL查询

转载 作者:行者123 更新时间:2023-12-04 05:31:29 26 4
gpt4 key购买 nike

我有一个带有事件列表的Java实体类UserBean:

@OneToMany
private List<EventBean> events;

EventBean具有Date变量:
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date eventDate;

现在在UserBean中,我想创建一个NamedQuery,它返回特定范围内的所有日期:
@NamedQuery(name="User.findEventsWithinDates",
query="SELECT u.events FROM UserBean u WHERE u.name = :name AND u.events.eventDate > :startDate AND u.events.eventDate < :endDate")

上面的查询虽然没有编译。我收到此错误:
The state field path 'u.events.eventDate' cannot be resolved to a valid type.

顺便说一下,我使用的是EclipseLink版本2.5.0.v20130507-3faac2b。

我该怎么做才能使此查询正常工作?谢谢。

最佳答案

路径u.events.eventDate是JPQL中的非法构造,因为不允许它通过集合值的路径表达式进行导航。在这种情况下,u.events是一个集合值的路径表达式。在JPA 2.0规范中,这是用以下单词来告知的:

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.



通过使用JOIN可以解决此问题:
SELECT distinct(u) 
FROM UserBean u JOIN u.events e
WHERE u.name = :someName
AND e.eventDate > :startDate
AND e.eventDate < :endDate

关于jpa - 在嵌套字段上使用WHERE进行JPQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com