简体   繁体   中英

How to properly get values in OneToMany relationship based on field of “many” side?

Let's say I have an entity

Restaurant

with field:

  @OneToMany(mappedBy = "restaurant", fetch = FetchType.LAZY) private Set<MenuEntry> menu; 

MenuEntry entity has next field:

 @Column(name = "date_time_created", nullable = false) private LocalDate dateCreated; 

which obviously represents date when entry was created.

I need to write JPQL query which would give me Restaurant where set of menus includes entries with specified dateCreated.

I've written method with next annotations:

 @EntityGraph(attributePaths = {"menu"}, type = EntityGraph.EntityGraphType.LOAD) @Query("SELECT r FROM Restaurant r WHERE r.id=:restaurantId AND r.menu IN (SELECT m FROM MenuEntry m WHERE m.dateCreated=:dateCreated)") 

But when I try to perform this query I have "malformed numeric constant" exception. Could you please explain me, how to properly get values in my situation ? Thank you!

Select r from Restaurant r JOIN r.menu m where m.dateCreated=:dateCreated and r.id=:restaurantId

Try this... This will work... No need to use inner query

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