假設有兩個實體 - 業主Hibernate HQL「加入的路徑!」 @ManyToOne關係
@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
。我想在寵物方面只有@ManyToOne
。
我錯過了什麼提示?
「預期路徑」意味着「所有者」無法訪問寵物,除非它將它們存儲在集合中。我相信你必須讓'所有者'保持寵物的集合。 –