简体   繁体   English

Java EE不使用IN运算符编译查询

[英]Java EE not compiling a query with the IN operator

I've written a query that works good if used into MySql Workbench, but it won't compile if used in my Java EE project! 我编写了一个查询,如果将其用于MySql Workbench,效果很好,但是如果在我的Java EE项目中使用,它将无法编译!

Here's the query : 这是查询:

@NamedQuery(name = "Book.findByCourse", query = "SELECT b FROM Book b WHERE b.teaching IN (SELECT T.id FROM Teaching T, Course C WHERE T.course = C.id AND C.id = :course)")

The query works fine, but I've got this error in my Java EE project : 该查询工作正常,但我的Java EE项目中出现此错误:

Error compiling the query [...], line 0, column 0: invalid IN expression argument [SubqueryNode Left: null Right: null], expected argument of type [entity.Teaching]. 编译查询[0]行0列时出错:无效的IN表达式参数[SubqueryNode左:null右:null],类型为[entity.Teaching]的预期参数。

What's wrong with it? 它出什么问题了?

First of all you are trying to select ids only in your subquery and then compare it to full pojo object (not sure if that's possible at all) 首先,您尝试仅在子查询中选择ID,然后将其与完整的pojo对象进行比较(不确定是否可以这样做)

In my opinion you should either got with native query (@NamedNativeQuery) to achieve what you want or use Criteria Builder like for example here 我认为您应该使用本机查询(@NamedNativeQuery)来实现所需的功能,或者使用Criteria Builder,例如此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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