我有以下情况:
@Entity
public class Definition {
...
@OneToMany(mappedBy = "productDefinition", cascade = { CascadeType.ALL })
@OrderBy("name")
private Set<Product> products = new LinkedHashSet<>();
....
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Product {
...
@ManyToOne(fetch = FetchType.LAZY)
private Definition productDefinition;
...
}
@Entity
public class FancyProduct extends Product {
}
@Entity
public class PlainProduct extends Product {
}
在我的 DAO 中,我有一个通用构建的标准查询,通过definitions
它应该提取每种特定类型产品的计数(在本例FancyProduct
中PlainProduct
分别为 )。当我在代码中执行 setJoin 时,如下所示:
SetJoin<T, ?> setJoin = from.joinSet(attributeName, JoinType.LEFT);
更新:
我试图提取创建查询的方式。它是从更广泛的背景中提取的,所以我希望它有任何意义:
// The result class is DefinitionDTO a DTO class
CriteriaQuery<R> query = builder.createQuery(resultClass);
// select from - the entity class (Definition in my case)
Root<T> from = query.from(entityClass);
...
SetJoin<T, ?> setJoin = from.joinSet(attributeName, JoinType.LEFT);
// add type restriction
Class<?> manyElemTypeClass = attribute.getManyElemTypeClass();
if (manyElemTypeClass != null &&!Modifier.isAbstract(manyElemTypeClass.getModifiers())) {
predicates.add(builder.or(setJoin.type().in(manyElemTypeClass), setJoin.type().isNull()));
}
Expression<Long> count = builder.countDistinct(setJoin);
count.alias(attributeName);
// select count
countSelections.add(count);
// order by count
orderByExpressions.add(count);
// always restrict data based on join
wherePath = setJoin;
...
所以当我只有一个孩子时有效,所以在这种情况下,如果我需要 just PlainProduct
,但是一旦我也添加FancyProduct
到结果 dto 中,它就不再起作用了。
生成的 SQL 如下所示:
select
filmmateri0_
count(distinct producepro3_.id) as col_6_0_,
from
ProductDefinition filmmateri0_
left outer join
(
select
id,
creationDate,
entityVersion,
key,
lastChangedDate,
deleted,
description,
produceMaterial_id,
1 as clazz_
from
FancyProduct
union
all select
id,
creationDate,
entityVersion,
key,
lastChangedDate,
deleted,
description,
produceMaterial_id,
2 as clazz_
from
PlainProduct
) producepro3_
on filmmateri0_.id=producepro3_.produceMaterial_id
所以在这种情况下,它只会计算 type 的所有元素Product
。setJoin
有没有办法指向join
一个特定的叶子孩子?