【发布时间】:2017-07-22 02:54:12
【问题描述】:
假设有两个实体——所有者
@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...
}
和宠物
@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...
}
其中一个所有者可以拥有多个宠物(原类已重命名),但一个宠物只能属于一个所有者。我想做的是找到拥有具有某些 id 的宠物的所有者,例如:
select Owner.ownerId, Owner.name from Owner inner join Pet on Owner.ownerId=Pet.ownerId where Pet.petId=3;
当在纯 SQL 中执行时,这可以正常工作。但是,我在 HQL 中尝试了这两个查询,它们都给出了错误Path expected for join!
select o from Owner o inner join Pet p on o.ownerId=p.owner.ownerId where p.petId= :petId
和
from Owner o join Pet p where p.petId= :petId
请注意,所有者中没有 @OneToMany 或 Collection<Pet> pets。我想在 Pet 端只使用 @ManyToOne。
关于我错过了什么的任何提示?
【问题讨论】:
-
"Path expected" 意味着
Owner没有办法访问这些宠物,除非它把它们存储在一个集合中。我相信你必须让Owner保留宠物的收藏。
标签: java sql hibernate jpa hql