Ali*_*aka 9 java hibernate jpa
我有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;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试按模型查询人物.即使表之间的连接是明确的,运行以下代码也会失败:
select mo from Person mo where mo.cars.model = ?
Run Code Online (Sandbox Code Playgroud)
错误是:
org.hibernate.QueryException: illegal attempt to dereference collection [...] with element property reference [model] [select mo from Person mo where mo.cars.model = ?]
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?
pla*_*ben 12
当已经定义了实体之间的关系时,您可以使用以下join fetch语法:
select mo from Person mo join fetch mo.cars c where c.model = ?
Run Code Online (Sandbox Code Playgroud)
mo.cars是一套.您无法访问Set的model属性,因为它没有.您需要加入:
select p from Person p
inner join p.cars car
where car.model = :model
Run Code Online (Sandbox Code Playgroud)
一如既往的相关文档.
| 归档时间: |
|
| 查看次数: |
9125 次 |
| 最近记录: |