【问题标题】:Hibernate HQL Projection Using Entities ManyToMany Definitions IssueHibernate HQL 投影使用实体多对多定义问题
【发布时间】:2014-08-20 07:28:16
【问题描述】:

问题:当我引用实体集合字段作为 HQL 语句的一部分时,HQL 查询未返回任何结果。它适用于一个 HQL 投影,例如:

select inc.categoryTypes as categoryTypes from IncidentEntity inc where (inc.id = :id105019)

categoryTypes 是 IncidentEntity 类字段之一(它是一个定义为 ManyToMany 连接的集合,如下所示)。这很好用,但是当我尝试引用另一个映射为 ManyToMany 连接的投影集合时会出现问题。

select inc.categoryTypes as categoryTypes, inc.consequences as consequences from IncidentEntity inc where (inc.id = :id105019)

只要我这样做,我就会得到一个空集。这意味着休眠生成的 SQL 查询没有返回任何内容。我已通过在 SQL 管理器中执行不返回任何结果的命令来验证这一点。

这里是 IncidentEntity:

/**
 * Database entity for the 'incidents' table records.<br>
 * Entity domain object is {@link nz.co.doltech.ims.shared.domains.Incident}
 * @author Ben Dol
 * 
 */
@javax.persistence.Entity(name = "incidents")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class IncidentEntity implements Entity {

    ...

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "incident_categorytype", joinColumns = { 
            @JoinColumn(name = "incident_id") }, 
        inverseJoinColumns = {
            @JoinColumn(name = "categorytype_id") 
    })
    private Set<CategoryTypeEntity> categoryTypes = new HashSet<CategoryTypeEntity>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "incident_consequence", joinColumns = { 
            @JoinColumn(name = "incident_id") }, 
        inverseJoinColumns = {
            @JoinColumn(name = "consequence_id") 
    })
    private Set<ConsequenceEntity> consequences = new HashSet<ConsequenceEntity>();

    ...

    public Set<CategoryTypeEntity> getCategoryTypes() {
        return categoryTypes;
    }
    public void setCategoryTypes(Set<CategoryTypeEntity> categoryTypes) {
        this.categoryTypes = categoryTypes;
    }

    public Set<ConsequenceEntity> getConsequences() {
        return consequences;
    }
    public void setConsequences(Set<ConsequenceEntity> consequences) {
        this.consequences = consequences;
    }

    ...
}

CategoryTypeEntity 关系定义:

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "categoryTypes")
private Set<IncidentEntity> incidents = new HashSet<IncidentEntity>();

ConsequenceEntity 关系定义:

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "consequences")
private Set<IncidentEntity> incidents = new HashSet<IncidentEntity>();

数据结构:

使用 Hibernate 3.6.10

也许我的定义设置错误,或者我在这里缺少 HQL 的限制,我不确定。将不胜感激我能在这里得到的任何帮助。谢谢!

问候, 本

【问题讨论】:

  • 我正在尝试生成这个,但在我的情况下,如果我同时选择多对多关系,我会得到列表。
  • 这真的很奇怪。你的多对多定义和我们这里设置的一样吗?也许我的关系定义缺少一些重要的东西。如果你可以检查的话,用关系定义的另一面更新了我的帖子。感谢您的回复!
  • 是的,一切都是一样的。在我的情况下,我有 Class1,它与 Class2 和 Class3 有很多关系。我已经通过 xml 完成了映射,但这并没有产生差异。
  • 不,仍然不知道为什么它不起作用...

标签: java sql hibernate jpa hql


【解决方案1】:

您知道您正在使用此查询生成笛卡尔积,对吧?

查询可以更好地可视化为:

select categoryTypes, consequences 
from IncidentEntity inc 
inner join inc.categoryTypes as categoryTypes
inner join inc.consequences as consequences
where (inc.id = :id105019)

由于您没有指定显式联接,因此假定 INNER JOIN 不是 LEFT JOIN。

让我们假设指定事件有类别。因此,此查询将返回此事件的类别,这也是您报告的:

select categoryTypes
from IncidentEntity inc 
inner join inc.categoryTypes as categoryTypes
where (inc.id = :id105019)

但是当没有结果时,INNER JOIN 将不返回任何结果,所以:

select categoryTypes, consequences 
from IncidentEntity inc 
inner join inc.consequences as consequences
where (inc.id = :id105019)

不会返回任何内容,但您的查询也会发生这种情况:

select categoryTypes, consequences 
from IncidentEntity inc 
inner join inc.categoryTypes as categoryTypes
inner join inc.consequences as consequences
where (inc.id = :id105019)

【讨论】:

  • 感谢您的回复,如何让 Hibernate 在 HQL 查询中假设 LEFT JOIN?
  • 在我的示例查询中,您需要用左替换内部,然后您将使用左连接。
  • 有没有办法让休眠假设这个?就像在关系定义中一样。
猜你喜欢
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 2023-04-11
  • 1970-01-01
相关资源
最近更新 更多