简体   繁体   English

Hibernate HQL“希望加入的路径!” @ManyToOne关系

[英]Hibernate HQL “Path expected for join!” @ManyToOne relationship

Suppose there are two entities - Owner 假设有两个实体-所有者

@Entity
@NamedQueries({
   @NamedQuery(name = "Owner.findOwnerForPetId", query = "select o from Owner o inner join Pet p on o.ownerId=p.owner.ownerId where p.petId= :petId")
})
public class Owner {

  @Id
  @Column(name = "ownerId")
  private Long ownerId;

  @Column
  private String name;

  // scaffolding code...
}

and Pet 和宠物

@Entity
public class Pet {

  @Id
  @Column(name = "petId")
  private Long petId;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ownerId")
  private Owner owner;

  @Column
  private String name;

  // scaffolding code...
}

where one Owner can have multiple pets (original class were renamed), but one Pet can only belong to one Owner. 其中一位拥有者可以拥有多只宠物(原始类被重命名),但是一只宠物只能属于一位拥有者。 What I would like to do is find the Owner that owns a Pet that has some id, like: 我想做的是找到拥有具有某些ID的宠物的所有者,例如:

select Owner.ownerId, Owner.name from Owner inner join Pet on Owner.ownerId=Pet.ownerId where Pet.petId=3;

This works fine when executed in pure SQL. 在纯SQL中执行时,此方法工作正常。 However, I have tried these two queries in HQL and they both give the error Path expected for join! 但是,我已经在HQL中尝试了这两个查询,并且它们都给出了Path expected for join!的错误Path expected for join!

select o from Owner o inner join Pet p on o.ownerId=p.owner.ownerId where p.petId= :petId

and

from Owner o join Pet p where p.petId= :petId

Note that there are no @OneToMany or Collection<Pet> pets in Owner. 请注意,所有者中没有@OneToManyCollection<Pet> pets I would like to do it with only a @ManyToOne on the Pet side. @ManyToOne在Pet一侧使用@ManyToOne来完成此操作。

Any hints on what I have missed? 关于我错过了什么的任何提示?

试试这个

  select o from Pet p inner join p.owner o where p.petId= :petId

When working with HQL you must use the relations between entities not just entities 使用HQL时,您必须使用实体之间的关系,而不仅仅是实体

so for INNER JOIN and LEFT JOIN for example you should use the relation direct 因此,例如对于INNER JOINLEFT JOIN ,您应使用直接关系

For example next are valid queries 例如接下来是有效的查询

SELECT o FROM Pet p inner join p.owner o WHERE p.petId= :petId (same as @rathna accepted answer) 从Pet中选择o内部联接p。 所有者o WHERE p.petId =:petId (与@rathna接受的答案相同)

SELECT p FROM Pet p WHERE p.owner.ownerId = :ownerId 从宠物p 那里选择p p.owner.ownerId =:ownerId

For the sake of completeness, if you need to LEFT JOIN but have the @ManyToOne attribute on the right side and thus can't specify a path, you can transform the query into a RIGHT JOIN with the same effect (ie not losing rows where the other table doesn't have matching rows and filtering the other table without losing null rows). 为了完整起见,如果您需要LEFT JOIN但在右侧具有@ManyToOne属性,因此无法指定路径,则可以查询转换为具有相同效果的RIGHT JOIN (即,不丢失行,另一个表没有匹配的行,并且在不丢失空行的情况下过滤了另一个表)。

Suppose you want to get all owners that don't have pets ignoring pets named Charly: 假设您想让所有没有宠物的主人都忽略名为Charly的宠物:

You can't specify 您无法指定

SELECT o
FROM Owner o
LEFT JOIN o.pet p (<-------- ERROR) WITH p.name != 'Charly'
GROUP BY o.ownerId
HAVING count(p.petId) = 0

But you could transform this into: 但是您可以将其转换为:

SELECT o
FROM Pet p
RIGHT JOIN p.owner o WITH p.name != 'Charly'
GROUP BY o.ownerId
HAVING count(p.petId) = 0

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

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