在一对多关系中指定子表的标准 [英] Specifying criteria for child table in One To Many Relationship

查看:76
本文介绍了在一对多关系中指定子表的标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有一对多关系的实体对象(A和B)。我正在使用JPA(Hibernate)来连接这些表并查询它们以获取特定的结果集,但是在获取我的结果时不会应用我为子表(B)指定的条件。这是我定义查询的方式:

  CriteriaBuilder builder = entityManager.getCriteriaBuilder(); 
CriteriaQuery< A> query = builder.createQuery(A.class);
Root< A> a = query.from(A.class);
加入< A,B> abJoined = a.join(A_.b);

query.distinct(true)
.where(builder.and(
builder.equal(a.get(A_.id),id),
builder .equal(a.get(A_.active),1),
builder.equal(a.get(A_.location),1011),
builder.equal(a.get(A_.status ),待定),

builder.equal(abJoined.get(B_.status),Not Moved),
builder.greaterThan(abJoined.get(B_.detailId) ,0)
));

当我调用 entityManager.createQuery(query).getResultList(); 我得到实体'A'的一个实例,但是当我尝试通过'A' a.getB()访问'B'时,两个标准我为 abJoined 指定的所有实例都未应用,并且我获得了连接到'A'的'B'的所有实例。我还需要做些什么来获得这些标准?如果不能应用标准,是否有推荐的方法从结果集中删除'B'的相应实例?



我可以提供更多代码或其他详细信息if必要的。

解决方案

查询用于选择查询必须返回哪个实体。但是A实体永远不会是仅包含其子集的子集的部分实体。它们将始终是A的完整实例,反映A在数据库中的状态,以及与A有关的B。

顺便说一句,即使这是可能的(这不是,并且明确地被JPA规范禁止),你的查询至少需要加载Bs以及使用fetch连接。否则,只有A的状态被查询返回,并且链接的Bs会被延迟加载。


I have two entity objects (A and B) that have a One-To-Many relationship. I am using JPA (Hibernate) to join these tables and query them for a specific result set, but the criteria I specify for the child table (B) are not applied when fetching my results. This is how I have defined the query:

CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<A> query = builder.createQuery(A.class);
Root<A> a = query.from(A.class);
Join<A, B> abJoined = a.join(A_.b);

query.distinct(true)
    .where(builder.and(
        builder.equal(a.get(A_.id), id),
        builder.equal(a.get(A_.active), 1),
        builder.equal(a.get(A_.location), 1011),
        builder.equal(a.get(A_.status), "Pending"),

        builder.equal(abJoined.get(B_.status), "Not Moved"),
        builder.greaterThan(abJoined.get(B_.detailId), 0)
    ));

When I call entityManager.createQuery(query).getResultList(); I get one instance of entity 'A', but when I try to access 'B' through 'A' a.getB() the two criteria that I had specified for abJoined are not applied and I get all instances of 'B' that are joined to 'A'. Is there something more I need to do to get these criteria applied? If the criteria cannot be applied, is there a recommended method for removing the corresponding instances of 'B' from the result set?

I can provide more code or other details if necessary.

解决方案

The query is used to select which A entities must be returned by the query. But the A entities will never be partial entities containing only a subset of their Bs. They will always be complete instances of A, reflecting the state of what A is in the database, and which B this A is related to.

BTW, even if this was possible (it's not, and explicitely forbidden by the JPA spec), your query would at least have to load the Bs as well, using a fetch join. Otherwise only the state of A is returned by the query, and the linked Bs are loaded lazily.

这篇关于在一对多关系中指定子表的标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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