QueryDsl - 基于别名的子查询,带有' WITH'条款

时间:2017-07-21 16:07:48

标签: java sql postgresql common-table-expression querydsl

我有这个SQL查询:

WITH tmv AS (
    SELECT *
    FROM test_materials_versions
    WHERE test_materials_versions.material_original_id = (
      SELECT material_original_id
      FROM test_materials_versions
      WHERE test_materials_versions.material_type = ?1 AND test_materials_versions.material_version_id = ?2)
),

all_accepted AS (
    SELECT DISTINCT tmv.* FROM tmv, atomic_objects ao
    WHERE tmv.material_version_id = ao.id AND ao.moderation_status = ?3)

SELECT * FROM all_accepted ORDER BY all_accepted.created_at DESC LIMIT ?4;

它可能会更好,但现在它很好,我希望它在我的4.1.4 querydsl查询中。 所以,我写了这个:

@Override
public List<TestMaterialVersionEntity> getMaterialVersionsOrderByDescCreationDate(
    @NonNull final Integer taskVersionId,
    @NonNull final MaterialType materialType,
    @NonNull final Integer moderationStatusId,
    @NonNull final Integer limit)
{
    JPASQLQuery<TestMaterialVersionEntity> query = new JPASQLQuery<>(this.em, new PostgreSQLTemplates());
    QTestMaterialVersionEntity tmv = new QTestMaterialVersionEntity("tmv");
    QTestMaterialVersionEntity allAccepted = new QTestMaterialVersionEntity("allAccepted");

    // todo switch
    QueryDslMaterialEntity materialEntity = null;
    if (materialType.equals(MaterialType.ATOMIC_OBJECT)) {
        materialEntity = qAtomicObjectWrapper;
    } else if (materialType.equals(MaterialType.TEST_SPECIFICATION)) {
        materialEntity = qTestSpecificationWrapper;
    }

    QTestMaterialVersionEntity testMaterialVersionEntity = QTestMaterialVersionEntity.testMaterialVersionEntity;
    query
        .with(
            tmv,
            JPAExpressions.selectFrom(testMaterialVersionEntity)
                .where(testMaterialVersionEntity.materialOriginalId.eq(
                    JPAExpressions.select(testMaterialVersionEntity.materialOriginalId)
                        .from(testMaterialVersionEntity)
                        .where(
                            testMaterialVersionEntity.materialType.eq(materialType.getValue()),
                            testMaterialVersionEntity.materialVersionId.eq(taskVersionId)
                        )
                    )
                )
        )
        .with(
            allAccepted,
            JPAExpressions.selectDistinct(tmv).from(tmv, materialEntity.getQEntity())
                .where(
                    tmv.materialVersionId.eq(materialEntity.getId()),
                    materialEntity.getModerationStatus().eq(moderationStatusId)
                )
        )
        .select(allAccepted)
        .from(allAccepted)
        .orderBy(new OrderSpecifier<>(Order.DESC, allAccepted.createdAt))
        .limit(limit);

    List<TestMaterialVersionEntity> result = query.fetch();
    return result;
}

但它混乱了我的别名&#34; tmv&#34;和#34; allAccepted&#34;。这来自fetch()

之前的调试查询对象
with tmv as (select testMaterialVersionEntity
from test_materials_versions testMaterialVersionEntity
where testMaterialVersionEntity.material_original_id = (select testMaterialVersionEntity.material_original_id
from test_materials_versions testMaterialVersionEntity
where testMaterialVersionEntity.material_type = ?1 and testMaterialVersionEntity.material_version_id = ?2)), allAccepted as (select distinct tmv
from test_materials_versions tmv, atomic_objects atomicObject
where tmv.material_version_id = atomicObject.id and atomicObject.moderation_status = ?3)
select {allAccepted.*}
from test_materials_versions allAccepted
order by allAccepted.created_at desc
limit ?4

请建议。

表:

CREATE TABLE public.test_materials_versions (
  id BIGINT PRIMARY KEY NOT NULL DEFAULT nextval('test_materials_versions_id_seq'::regclass),
  material_original_id INTEGER NOT NULL,
  material_version_id INTEGER NOT NULL,
  material_type CHARACTER VARYING(255) NOT NULL,
  created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);

CREATE TABLE public.atomic_objects (
id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('atomic_objects_id_seq'::regclass),
moderation_status INTEGER DEFAULT 1,
);

material_original_id,material_version_id是fkeys to atomic_objects.id

0 个答案:

没有答案