简体   繁体   English

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;

        [...]

    }

Given the following structure, I'd like to perform the HQL or CriteriaQuery equivalent of the following SQL: 鉴于以下结构,我想执行以下SQL的HQL或CriteriaQuery等价物:

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. 我想找回与给定人员相关联的位置列表,其创建日期在给定人员之后。

Thanks in advance! 提前致谢!

Mark 标记

Edit***: I have edited the SQL, as it was kinda misleading. 编辑***:我编辑了SQL,因为它有点误导。 I don't want to query for the locations independently. 我不想独立查询位置。

This is not possible, you cannot query an Embeddable . 这是不可能的,你不能查询一个Embeddable From the JPA Wikibook: 来自JPA Wikibook:

Embedded Collections 嵌入式集合

An ElementCollection mapping can be used to define a collection of Embeddable objects. ElementCollection映射可用于定义Embeddable对象的集合。 This is not a typical usage of Embeddable objects as the objects are not embedded in the source object's table, but stored in a separate collection table. 这不是Embeddable对象的典型用法,因为对象未嵌入源对象的表中,而是存储在单独的集合表中。 This is similar to a OneToMany , except the target object is an Embeddable instead of an Entity . 这类似于OneToMany ,但目标对象是Embeddable而不是Entity This allows collections of simple objects to be easily defined, without requiring the simple objects to define an Id or ManyToOne inverse mapping. 这允许容易地定义简单对象的集合,而不需要简单对象来定义IdManyToOne逆映射。 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还可以覆盖其集合的映射或表,因此您可以让多个实体引用相同的Embeddable类,但每个实体都将其依赖对象存储在单独的表中。

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. 使用ElementCollection而不是OneToMany的限制是无法独立于父对象查询 ,保留,合并目标对象。 They are strictly privately-owned (dependent) objects, the same as an Embedded mapping. 它们是严格的私有(依赖)对象,与Embedded映射相同。 There is no cascade option on an ElementCollection , the target objects are always persisted, merged, removed with their parent. ElementCollection上没有级联选项,目标对象始终与父级一起保持,合并,删除。 ElementCollection still can use a fetch type and defaults to LAZY the same as other collection mappings. ElementCollection仍然可以使用fetch类型,默认为LAZY,与其他集合映射相同。

To achieve what you want, use a OneToMany and an Entity instead of an ElementCollection and an Embeddable . 要实现您的目标,请使用OneToManyEntity而不是ElementCollectionEmbeddable Or change your approach and query the Person . 或者更改您的方法并查询Person

The key phrase in Pascal's reply is Pascal回复中的关键词是

the target objects cannot be queried, persisted, merged independently of their parent object 目标对象无法独立于其父对象进行查询,保持,合并

As you are dependent on the parent object, you should be able to do this using something like ... 由于你依赖于父对象,你应该可以使用类似的东西来做到这一点。

SELECT p FROM PERSON, IN (p.locations) WHERE p.id = ?1 AND locations = ?2

(Based on the reply at Execute "MEMBER OF" query against 'ElementCollection' Map fields in JP-QL (JPA 2.0) - which is actually a Map @ElementCollection which was what I was looking for an answer to!) (根据执行“MEMBER OF”的回复查询JP-QL(JPA 2.0)中的'ElementCollection'地图字段 - 这实际上是一个Map @ElementCollection,这就是我正在寻找的答案!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM