JPQL查询未返回预期结果

时间:2021-02-01 15:29:15

The following JPQL query is not returning results. How do I alter it so that it returns the results that are expected?

以下JPQL查询未返回结果。如何更改它以便返回预期的结果?

@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.

注意:解决方案是更改描述类中manytoone关系中的一个joincolumns的名称。但我正在将下面的答案中的一个标记为已被接受,因为该人投入了大量时间来帮助我。

2 个解决方案

#1


1  

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类具有类似私有概念的概念:

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. Consider revising that, you may want to make Concept has only one Description.

还有一件事看起来很可疑 - 概念和描述与一对多关系绑定。考虑修改,您可能希望使Concept只有一个描述。

#2


2  

The query generated by hibernate is fine. Let's analyze this step by step.

hibernate生成的查询很好。让我们一步一步地分析。

  • We have a SnomedDescription entity that has a complex key made of two columns: id and effectiveTime.
  • 我们有一个SnomedDescription实体,它有一个由两列组成的复杂键:id和effectiveTime。

  • The SnomedDescription entity has a @ManyTwoOne relationship with another entity named SnomedConcept.
  • SnomedDescription实体与另一个名为SnomedConcept的实体具有@ManyTwoOne关系。

  • SnomedConcept has also a complex key. 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. Which is weird actually, because that would mean @OneToOne relationship should be more suitable, like @Alex Malev suggested (or the mapping is defined incorrectly). 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.
  • SnomedConcept也有一个复杂的密钥。从你的问题我们不确定它是由哪些列组成的,但是从@ManyTwoOne关系定义我们可以假设它也是id和effectiveTime。这实际上是奇怪的,因为这意味着@OneToOne关系应该更合适,就像@Alex Malev建议的那样(或者映射定义不正确)。基本上我们不能有两个具有相同id和effectiveTime的SnomedDescriptions,因此一次最多只有一个SnomedDescription与一个SnomedConcept相关联。

  • 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和effectiveTime,并且最多匹配一行。

  • 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 - name =“effectiveTime” - 就可以了。

#1


1  

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类具有类似私有概念的概念:

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. Consider revising that, you may want to make Concept has only one Description.

还有一件事看起来很可疑 - 概念和描述与一对多关系绑定。考虑修改,您可能希望使Concept只有一个描述。

#2


2  

The query generated by hibernate is fine. Let's analyze this step by step.

hibernate生成的查询很好。让我们一步一步地分析。

  • We have a SnomedDescription entity that has a complex key made of two columns: id and effectiveTime.
  • 我们有一个SnomedDescription实体,它有一个由两列组成的复杂键:id和effectiveTime。

  • The SnomedDescription entity has a @ManyTwoOne relationship with another entity named SnomedConcept.
  • SnomedDescription实体与另一个名为SnomedConcept的实体具有@ManyTwoOne关系。

  • SnomedConcept has also a complex key. 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. Which is weird actually, because that would mean @OneToOne relationship should be more suitable, like @Alex Malev suggested (or the mapping is defined incorrectly). 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.
  • SnomedConcept也有一个复杂的密钥。从你的问题我们不确定它是由哪些列组成的,但是从@ManyTwoOne关系定义我们可以假设它也是id和effectiveTime。这实际上是奇怪的,因为这意味着@OneToOne关系应该更合适,就像@Alex Malev建议的那样(或者映射定义不正确)。基本上我们不能有两个具有相同id和effectiveTime的SnomedDescriptions,因此一次最多只有一个SnomedDescription与一个SnomedConcept相关联。

  • 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和effectiveTime,并且最多匹配一行。

  • 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 - name =“effectiveTime” - 就可以了。