简体   繁体   English

Hibernate返回错误的结果集并生成错误的查询

[英]Hibernate returns wrong result set and generates wrong query

I have a table which can referance on itself. 我有一张可以参考的表格。

organisation(id,child_from_Org, ...)

Hibernate Mapping 休眠映射

@Entity
@Table(name = "organisation")
public class Organisation implements java.io.Serializable {

  private Integer id;
  private Organisation organisation;
  private ...

  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "id", unique = true, nullable = false)
  public Integer getId() {
    return this.id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "child_from_Org")
  public Organisation getOrganisation() {
    return this.organisation;
  }

  public void setOrganisation(Organisation organisation) {
    this.organisation = organisation;
  }
  ...
}

If i execute aa hibernate query like "from Organisation", then i'll get all organisations. 如果我执行一个休眠查询,例如“来自组织”,那么我将获得所有组织。

from Organisation o where o.id = 1

Then i get the organisation with id 1 然后我得到ID为1的组织

from Organisation o where o.id = 1 or o.organisation.id = 1

Returns organisation with id 1 and all child organisations 返回ID为1的组织和所有子组织

from Organisation o where o.id = 1 or o.organisation.id = 1 or o.organisation.organisation.id = 1

Does not return the organisation with id 1!!!! 不返回ID为1的组织! Why? 为什么? I tried all possible combinations with braclets (() or () or ()). 我尝试了所有与手镯(()或()或())的组合。

I looks like a wrong SQL is getting generated from hibernate: 我似乎从休眠生成了错误的SQL:

select  organisati0_.id as id470_,
  organisati0_.child_from_Org as ist21_470_,70_,
  ,...
 from
  organisation organisati0_ cross 
 join
  organisation organisati1_ 
 where
  organisati0_.child_from_Org=organisati1_.id 
  and (
   organisati0_.id=1 
   or organisati0_.child_from_Org=1 
   or organisati1_.child_from_Org=1
  )

How can i resolve that problem? 我该如何解决这个问题?

Because you have an implicit join from Organisation to itself, and in hibernate implicit joins are inner joins: 因为您有一个从Organisation到其自身的隐式联接,并且在休眠状态下,隐式联接就是内部联接:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins-forms http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins-forms

The implicit form does not use the join keyword. 隐式形式不使用join关键字。 Instead, the associations are "dereferenced" using dot-notation. 而是使用点符号“取消引用”关联。 implicit joins can appear in any of the HQL clauses. 隐式联接可以出现在任何HQL子句中。 implicit join result in inner joins in the resulting SQL statement. 隐式联接会在结果SQL语句中进行内部联接。

Try explicit left joins, eg: 尝试显式左联接,例如:

select o from Organisation as o left join o.organisation as parent left join parent.organisation as grandparent where o.id = 1 or parent.id = 1 or grandparent.id = 1

( NOTE: I've assumed organization.organisation is a parent relationship. If that's not the case you can(and should) replace the parent and grandparent aliases with something more appropriate) 注意:我假设organization.organisation是父级关系。如果不是这种情况,则可以(并且应该)用更合适的名称替换parentgrandparent别名)

EDITED TO ADD: 编辑添加:

It seems what's happening is that the inner join is ruling out org with id of 1, probably because org with id of 1 has a null organisation 似乎正在发生的事情是内部联接排除了ID为1的组织,这可能是因为ID为1的organisation为空

EDITED AGAIN TO ADD: Added select per comment below. 再次编辑:在下面添加了按评论选择。

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

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