如何使用JPA条件API按透视值过滤/排序?

时间:2018-07-19 09:05:39

标签: java jpa criteria-api

比方说我们有这样的实体(跳过吸气剂/设置剂,可见性参数,注释等)

class TargetEntity {
 Integer id;
 List <Attribute> attributes;// one to many
 }

class Attribute {
 Integer id;
 AttributeType type;//many to one
String value;
}

class AttributeType{
Integer id;
String code;
String description;
}

我想同时选择所有不同的TargetEntities(通过和):

  1. 具有等于A的属性值,其中属性类型代码等于X(如果实体完全不具有带有此类代码的属性,则不应通过此条件)
  2. 当属性代码等于Y时,属性值不等于B(如果实体完全不具有带有此类代码的属性,则应通过此条件)

并按属性值对它们进行排序,其中属性代码等于Z(不存在该属性应视为空值)。

为此,我必须使用JPA标准API,并且无法向数据库添加任何视图。

谁能解释这种与标准API的透视相关的使用?

UPD:进行类似选择的sql查询大致如下:

select tet.* from target_entity_table tet
left join (
select attr.target_entity_table_id as hid, attr_type.code as code, attr.attribute_value as value from attribute_table attr 
inner join attribute_type_table attr_type on attr.attribute_id = attr_type.id
) X on X.hid = tet.id and X.code = 'X'
left join (
select attr.target_entity_table_id as hid, attr_type.code as code, attr.attribute_value as value from attribute_table attr 
inner join attribute_type_table attr_type on attr.attribute_id = attr_type.id
) Y on Y.hid = tet.id and Y.code = 'Y'
left join (
select attr.target_entity_table_id as hid, attr_type.code as code, attr.attribute_value as value from attribute_table attr 
inner join attribute_type_table attr_type on attr.attribute_id = attr_type.id
) Z on Z.hid = tet.id and Z.code = 'Z'
where x.value = 'A' and Y.value != 'B'
order by z.value asc

0 个答案:

没有答案