简体   繁体   English

JPQL查询未返回预期结果

[英]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. 让我们逐步分析这一点。

  • We have a SnomedDescription entity that has a complex key made of two columns: id and effectiveTime . 我们有一个具有由两列的复合键SnomedDescription实体:IDeffectiveTime。
  • The SnomedDescription entity has a @ManyTwoOne relationship with another entity named SnomedConcept. SnomedDescription实体与另一个名为SnomedConcept的实体具有@ManyTwoOne关系。
  • SnomedConcept has also a complex key. SnomedConcept还有一个复杂的密钥。 From your question we are not sure what columns is it made of, but from the @ManyTwoOne relationship definition we can assume that it is also id and effectiveTime . 从你的问题,我们不知道什么是列它制成的,但是从@ManyTwoOne关系定义我们可以认为它也是IDeffectiveTime。 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. 基本上,我们不能有两个具有相同id和有效时间的SnomedDescription,因此一次最多只能有一个SnomedConcept关联一个SnomedDescription。
  • 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. 对我来说,还有另外一件事情看起来很可疑- ConceptDescription存在一对多关系。 Consider revising that, you may want to make Concept has only one Description . 考虑修改一下,您可能希望使Concept仅具有一个Description

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

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