JPA 2 -- 在 CriteriaQuery 中使用 @ElementCollection [英] JPA 2 -- Using @ElementCollection in CriteriaQuery

查看:33
本文介绍了JPA 2 -- 在 CriteriaQuery 中使用 @ElementCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    @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 of Embeddable objects. 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. This is similar to a OneToMany, except the target object is an Embeddable instead of an Entity. This allows collections of simple objects to be easily defined, without requiring the simple objects to define an Id or ManyToOne 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.

要实现您想要的效果,请使用 OneToManyEntity 而不是 ElementCollectionEmbeddable.或者改变你的方法并查询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屋!

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