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.
推荐答案
这是不可能的,你不能查询嵌入式
。来自JPA Wikibook:
This is not possible, you cannot query an Embeddable
. From the JPA Wikibook:
嵌入式集合
一个
ElementCollection
映射可以使用
定义
Embeddable
对象的集合。这不是
典型用法Embeddable
对象
,因为对象未嵌入
源对象的表中,但存储在
单独的收集表。这是
类似于OneToMany
,但
目标对象是可嵌入
而不是实体
。这允许简单对象的
集合容易定义
,而不需要
简单对象来定义Id
或
ManyToOne
逆映射。
ElementCollection
也可以覆盖
映射或表
集合,这样你就可以有多个
实体引用相同的可嵌入
类,但每个都将它们的
依赖对象存储在一个单独的表中。
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.
使用
<$的限制c $ c> ElementCollection 而不是
OneToMany
是目标
对象无法查询,
持续存在,独立于
合并其父对象。它们是严格的
私有(依赖)对象,
与 Embedded
映射相同。
在
ElementCollection
上没有级联选项,目标
对象始终保持,合并,
随其删除家长。
ElementCollection
仍然可以使用
fetch类型,默认为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
和实体
而不是 ElementCollection
和 Embeddable
。或者更改您的方法并查询人
。
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屋!