0

我有一个 CriteriaBuilder 并试图创建一个谓词来限制 CollectionAttribute 中的结果。我想说的是获取此集合中的所有实体,其中实体的属性 A 等于 x。例如,我有很多 People 实体。他们有一系列职称(以前的和现在的),我想知道所有拥有“银行家”头衔的人。这是一个例子:

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Object> query = builder.createQuery();
Root<MheAreaLocation> root = query.from(MheAreaLocation.class);

Predicate p2 = builder.equal(root.get(Jobs_.jobs).get(Jobs_.titles), "banker");

TypedQuery<Object> q = em.createQuery(query);
List<Object> resultList = q.getResultList();
...

任何帮助都会很棒。我正在努力寻找有关 CollectionAttribute 的很多信息并在谓词中使用它们......当我在谓词中使用下面的连接时,我不断得到空值:-( 谢谢

这是我的实际代码:

CriteriaBuilder builder = em.getCriteriaBuilder();
// CriteriaQuery<Tuple> query = builder.createQuery();
CriteriaQuery<Object> query = builder.createQuery();
Root<MheAreaLocation> mheAreaLocationRoot = query.from(MheAreaLocation.class);

CollectionJoin<MheLocation, AtlasLocationGroupMap> join = mheAreaLocationRoot.join(MheAreaLocation_.childLocation).join(MheLocation_.atlasLocationGroupMapCollection);
// .join(AtlasLocationGroupMap_.atlasLocationGroup, JoinType.INNER);

Predicate p1 = builder.equal(mheAreaLocationRoot.get(MheAreaLocation_.parentLocation).get(MheLocation_.mheLocId), "AZP1B");
// Predicate p2 = builder.equal(mheAreaLocationRoot.get(MheAreaLocation_.childLocation).get(MheLocation_.atlasLocationGroupMapCollection).);
Predicate p2 = builder.equal(join.get(AtlasLocationGroupMap_.atlasLocationGroup).get(AtlasLocationGroup_.locationGroupType), "NEXT_STATION");

// query.where(builder.and(e1, e2));

// mheAreaLocationRoot.fetch(MheAreaLocation_.childLocation);
// join.fetch(MheLocation_.atlasLocationGroupMapCollection);

// query.multiselect(mheAreaLocationRoot.get(MheAreaLocation_.parentLocation),
// mheAreaLocationRoot.get(MheAreaLocation_.childLocation));
// query.select(builder.tuple(join.get(AtlasLocationGroupMap_.mheLocation)));

TypedQuery<Object> q = em.createQuery(query);
List<Object> resultList = q.getResultList();
...

其他类(如果需要我可以添加更多):

@StaticMetamodel(MheLocation.class)
public class MheLocation_ { 
   public static volatile CollectionAttribute<MheLocation, AtlasLocationGroupMap> atlasLocationGroupMapCollection;
   public static volatile SingularAttribute<MheLocation, String> mheLocId;
}
4

1 回答 1

0

以这个具有 Farm 和 Animal 类的示例为例(Farm 有一个集合<Animal>),我们想对 Animal 的名称强加一个标准。我认为与您的不完全相同,但可能会为您指明正确的方向

CriteriaBuilder qb = emf.getCriteriaBuilder();
CriteriaQuery<Farm> crit = qb.createQuery(Farm.class);
Root<Farm> candidate = crit.from(Farm.class);
candidate.alias("f");
crit.select(candidate);

Metamodel model = emf.getMetamodel();
ManagedType farmType = model.managedType(Farm.class);
Attribute animalAttr = farmType.getAttribute("animals");
Join animalJoin = candidate.join((ListAttribute)animalAttr);
animalJoin.alias("a");

Path nameField = animalJoin.get("name");
Predicate nameEquals = qb.equal(nameField, "Woolly Sheep");
crit.where(nameEquals);

等于 SELECT f FROM org.jpox.samples.annotations.one_many.bidir.Farm f JOIN f.animals a WHERE (a.name = 'Woolly Sheep')

高温高压

于 2010-10-08T16:47:29.643 回答