使用JPA关系查询时,非法尝试取消对集合的引用 [英] Illegal attempt to dereference collection when querying using JPA relations

查看:137
本文介绍了使用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;
}

我正在尝试按模型查询人员.即使表之间的连接很清楚,运行以下代码也会失败:

I'm trying to query people by model. Running the following code fails, even though the connections between the tables are clear:

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 = ?]

您知道如何解决该问题吗?

Any idea how to resolve the issue?

推荐答案

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

When the relationship between the entities is already defined, you can use the join fetch syntax:

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

这篇关于使用JPA关系查询时,非法尝试取消对集合的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆