gpt4 book ai didi

java - 使用 JPA 关系查询时非法尝试取消引用集合

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:09:24 26 4
gpt4 key购买 nike

我有 2 个类:

@Table(name = "PEOPLE")
@Entity
class Person {
@OneToMany(mappedBy = "owner")
Set<Car> cars;
}
@Table(name = "CARS")
@Entity
class Car {
@ManyToOne
@JoinColumn(name = "OWNER_ID", referencedColumnName = "ID")
Person owner;
@Column(name = "MODEL")
String model;
}

我正在尝试按模型查询人员。运行以下代码失败,即使表之间的联系很清楚:

select mo from Person mo where mo.cars.model = ?

错误是:

org.hibernate.QueryException: illegal attempt to dereference collection [...] with element property reference [model] [select mo from Person mo where mo.cars.model = ?]

知道如何解决这个问题吗?

最佳答案

当实体之间的关系已经定义好后,可以使用join fetch语法:

select mo from Person mo join fetch mo.cars c where c.model = ?

关于java - 使用 JPA 关系查询时非法尝试取消引用集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com