[英]JPQL - ManyToMany Self reference
I'm having a problem to make a query with JPQL. 我在用JPQL进行查询时遇到问题。 I have one table with reference himself with Many to Many:
我有一张桌子供许多人参考:
@Entity
@Table(name = "item")
public class Item extends BaseModel implements Serializable {
@Column(name = "id")
public INteger id;
@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
public Set<Item> itemsRelated;
}
And I'm trying to make this query: 我正在尝试进行以下查询:
Select * from Item where itemsRelated=null
or Select * from Item where itemsRelated is null 或从itemRelated为null的项目中选择*
But its throwled this Exception: 但是它抛出了这个异常:
play.exceptions.JavaExecutionException: Error while executing query from Item where itemsRelated=null order by createDate desc : Unknown column 'qualified' in 'where clause' at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237) at Invocation.HTTP Request(Play!) play.exceptions.JavaExecutionException: 从Item执行查询时出错, 其中itemRelated = null order by createDate desc :在Invocation.HTTP处play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)的“ where子句”中的未知列“ qualified”请求(玩!)
Can anyone help me? 谁能帮我?
UPDATE: 更新:
Java call: Java调用:
Item.find(query.toString()+" order by createDate desc").fetch(page, itemsPerPage);
At this point "query" has " itemsRelated is null" 此时,“查询”具有“ itemsRelated为空”
itemsRelated
is a collection and you have to check the size of the collection - don't expect it to be null
. itemsRelated
是一个集合,您必须检查集合的大小-不要期望它为null
。 Try 尝试
select i from Item i where i.itemsRelated is empty
See http://docs.oracle.com/cd/E17904_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_empty_comp 参见http://docs.oracle.com/cd/E17904_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_empty_comp
Edit: completed query with alias 编辑:使用别名完成查询
I'm not a jpql expert, but the syntax is null
and is not null
should work. 我不是jpql专家,但是语法
is null
,但is not null
应该可以。 Have you tried 你有没有尝试过
Select * from Item where itemsRelated is null
http://docs.oracle.com/cd/E17904_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_null http://docs.oracle.com/cd/E17904_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_null
Successful! 成功了!
The soluction was to use INNER JOIN, lets se: 解决方案是使用INNER JOIN,让我们:
id NOT IN (SELECT DISTINCT(i.id) FROM Item i INNER JOIN i.itemsRelated ii)
the result is: 结果是:
Select * from Item where id NOT IN (SELECT DISTINCT(i.id) FROM Item i INNER JOIN i.itemsRelated ii)
Thanks for everyone! 谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.