JPA 2 -- 在 CriteriaQuery 中使用 @ElementCollection [英] JPA 2 -- Using @ElementCollection in CriteriaQuery
问题描述
@Entity
public class Person {
@ElementCollection
private List<Location> locations;
[...]
}
@Embeddable
public class Location {
private Integer dummy;
private Date creationDate;
[...]
}
鉴于以下结构,我想执行以下 SQL 的 HQL 或 CriteriaQuery 等效项:
Given the following structure, I'd like to perform the HQL or CriteriaQuery equivalent of the following SQL:
SELECT
l.*
FROM
Location l
INNER JOIN
Person p ON (p.id = l.person_id)
WHERE
p.id = ? AND l.creationDate > ?
我想取回与给定人员相关联且其创建日期在给定日期之后的位置列表.
I want to get back a list of Locations that are associated with the given person and whose creationDate is after the given one.
提前致谢!
标记
编辑***:我已经编辑了 SQL,因为它有点误导.我不想独立查询位置.
Edit***: I have edited the SQL, as it was kinda misleading. I don't want to query for the locations independently.
推荐答案
这是不可能的,您不能查询 Embeddable
.来自 JPA 维基教科书:
This is not possible, you cannot query an Embeddable
. From the JPA Wikibook:
ElementCollection
映射可以是用于定义一个集合Embeddable
对象.这不是一个Embeddable
对象的典型用法因为对象没有嵌入源对象的表,但存储在单独的收集表.这是类似于 OneToMany
,除了目标对象是一个 Embeddable
而不是 Entity
.这允许简单对象的集合容易定义,不需要定义 Id
的简单对象或ManyToOne
逆映射.ElementCollection
也可以覆盖映射或表集合,所以你可以有多个实体引用相同的 Embeddable类,但让每个人存储他们的单独表中的依赖对象.
Embedded Collections
An
ElementCollection
mapping can be used to define a collection ofEmbeddable
objects. This is not a typical usage ofEmbeddable
objects as the objects are not embedded in the source object's table, but stored in a separate collection table. This is similar to aOneToMany
, except the target object is anEmbeddable
instead of anEntity
. This allows collections of simple objects to be easily defined, without requiring the simple objects to define anId
orManyToOne
inverse mapping.ElementCollection
can also override the mappings, or table for their collection, so you can have multiple entities reference the same Embeddable class, but have each store their dependent objects in a separate table.
使用一个的限制ElementCollection
而不是一个OneToMany
是目标无法查询对象,持久化,独立于合并他们的父对象.他们严格私有(依赖)对象,与 Embedded
映射相同.没有级联选项ElementCollection
,目标对象总是被持久化、合并、与他们的父母一起移除.ElementCollection
仍然可以使用一个获取类型并默认为 LAZY与其他集合映射相同.
The limitations of using an
ElementCollection
instead of a
OneToMany
is that the target
objects cannot be queried,
persisted, merged independently of
their parent object. They are strictly
privately-owned (dependent) objects,
the same as an Embedded
mapping.
There is no cascade option on an
ElementCollection
, the target
objects are always persisted, merged,
removed with their parent.
ElementCollection
still can use a
fetch type and defaults to LAZY the
same as other collection mappings.
要实现您想要的效果,请使用 OneToMany
和 Entity
而不是 ElementCollection
和 Embeddable
.或者改变你的方法并查询Person
.
To achieve what you want, use a OneToMany
and an Entity
instead of an ElementCollection
and an Embeddable
. Or change your approach and query the Person
.
这篇关于JPA 2 -- 在 CriteriaQuery 中使用 @ElementCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!