[英]JPQL query not returning expected results
The following JPQL query is not returning results. 以下JPQL查询未返回结果。 How do I alter it so that it returns the results that are expected?
如何更改它以使其返回预期的结果?
@SuppressWarnings("unchecked")
public Collection<Description> findDescriptionsForConcept(Concept conc) {
System.out.println("in JpaSnomedRepository(), conc.getConceptPk().getId() is: "+conc.getConceptPk().getId());;
Query query = this.em.createQuery("SELECT descr FROM Description descr WHERE descr.concept =:cid");
query.setParameter("cid", conc);
return query.getResultList();
}
NOTE: The solution was to change the name of one of the joincolumns in the manytoone relationship in the description class. 注意:解决方案是更改描述类中多对多关系中连接列之一的名称。 But I am marking one of the answers below as accepted because the person invested a lot of time trying to help me.
但是,我将以下答案之一标记为“已接受”,因为该人投入了大量时间来帮助我。
The query generated by hibernate is fine. 休眠生成的查询很好。 Let's analyze this step by step.
让我们逐步分析这一点。
@ManyTwoOne
relationship with another entity named SnomedConcept. @ManyTwoOne
关系。 @ManyTwoOne
relationship definition we can assume that it is also id and effectiveTime . @ManyTwoOne
关系定义我们可以认为它也是ID和effectiveTime。 Which is weird actually, because that would mean @OneToOne
relationship should be more suitable, like @Alex Malev suggested (or the mapping is defined incorrectly). @OneToOne
关系应该更适合,例如@Alex Malev建议(或映射定义不正确)。 Basically we can't have two SnomedDescriptions of same id and effectiveTime, so there will be at most one SnomedDescription assotiated with a single SnomedConcept at a time. Why is the generated query fine? 为什么生成的查询很好? Because
因为
DESCRIPTION.CONCEPT.CONCEPTPK.ID = DESCRIPTION.ID DESCRIPTION.CONCEPT.CONCEPTPK.ID = DESCRIPTION.ID
That's how the relationship is defined! 这就是定义关系的方式!
If the JPQL was something like "SELECT descr FROM SnomedDescription descr WHERE descr.concept = :concept"
, the generated query would have two constraints: id and effectiveTime and would match at most one row. 如果JPQL类似于
"SELECT descr FROM SnomedDescription descr WHERE descr.concept = :concept"
,则生成的查询将具有两个约束: id和有效时间,并且最多匹配一行。
If you still would like to utilise @ManyToOne relationship, I believe that just removing the second @JoinColumn - of name = "effectiveTime" - would do the trick. 如果您仍然想利用@ManyToOne关系,我相信只需删除第二个@JoinColumn-名称=“ effectiveTime”-就能解决问题。
Try changing method a little bit, setting id
as parameter, not the whole Concept
. 尝试稍微改变方法,将
id
设置为参数,而不是整个Concept
。
This code assumes your SnomedDescription
class has something like private Concept concept
: 此代码假定您的
SnomedDescription
类具有类似于private Concept concept
:
Query query = this.em.createQuery("SELECT descr FROM SnomedDescription descr WHERE descr.concept.conceptPk.id =:cid");
query.setParameter("cid", conc.getConceptPk().getId());
Also one more thing looks suspicious for me - Concept
and Description
are bound with one-to-many relation. 对我来说,还有另外一件事情看起来很可疑-
Concept
和Description
存在一对多关系。 Consider revising that, you may want to make Concept
has only one Description
. 考虑修改一下,您可能希望使
Concept
仅具有一个Description
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.