java - 如何使用 JPA 标准 API 按透视值过滤/排序?
问题描述
假设我们有这样的实体(跳过 getter/setter、可见性参数、注释等)
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:
- 具有等于 A 的属性值,其中属性类型代码等于 X(如果实体根本没有具有此类代码的属性,则不应通过此标准)
- 具有不等于 B 的属性值,其中属性代码等于 Y(如果实体根本没有具有此类代码的属性,则应通过此标准)
并按属性值对它们进行排序,其中属性代码等于 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
解决方案
推荐阅读
- arrays - 为什么在此代码中出现数组超出范围错误?
- java - 如何使用 Spring Boot 应用程序和 Spock 测试在运行时更改服务器端口
- php - Codeigniter MY_Controller 未将变量发送到视图
- java - 如何使用 Spring Boot 配置 Selenium WebDriver 以进行 UI 测试?
- django - Python 抛出的带有 TestCase 继承的警告 - “需要更多值来解包”
- c# - 在视野内生成,但始终低于特定高度
- swift - 如何向 Firebase 数组添加另一个键/值
- maven - Maven Checkstyle 不允许导入
- node.js - 在 Mac OS 上使用带有电子的 sqlite3 的问题
- python - 我正在尝试使用 Python 正则表达式解析一些日志文件,但出现以下错误:行继续符后出现意外字符