hibernate 使用Querydsl并行 Package 查询

tvmytwxo  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(114)

我正在使用Oracle和QueryDsl版本5.0.0。我有以下Oracle SQL查询:

SELECT /*+ parallel(2)*/ *
FROM (
WITH temp AS (
SELECT name1, name2 FROM names
--Dynamic filters are added here from client)
SELECT name1 AS name FROM temp
UNION ALL (SELECT name2 AS name FROM temp)
);

过滤器示例-其中name1是John或Adam:

[…]
WITH temp AS (
SELECT name1, name2
FROM names
WHERE name1 IN (‘John’, ‘Adam’))
[…]

我的代码:
请注意,this.getConditions(Request request)返回包含客户端发送的过滤器的BooleanBuilder。

@Autowired
private JdbcTemplate jdbcTemplate;
private final QNames namesTable = QNames.names;
private final QNames withTable = new Qnames("with");

public List<String> getNames(Request request) throw Exception {
    Connection conn = this.jdbcTemplate.getDataSource().getConnection();

    OracleQuery<Names> withQuery = new OracleQuery<Names>(conn)
        .select(Projections.constructor(
            Names.class,
            this.namesTable.name1.as(this.withTable.name1),
            this.names.name2.as(this.withTable.name2))
        )
        .from(this.names)
        .where(this.getConditions(request));

    OracleQuery<String> name1Query = new OracleQuery<String>(conn)
        .select(this.withTable.name1.as("name"))
        .from(this.withTable);

    OracleQuery<String> name2Query = new OracleQuery<String>(conn)
        .select(this.withTable.name2.as(“name”))
        .from(this.withTable);

    OracleQuery<String> fullQuery = new OracleQuery<String>(conn)
        .with(this.withTable, withQuery);

    fullQuery.unionAll(name1Query, name2Query);

    return fullQuery.fetch();
}

我不知道如何在这段代码中添加并行。
我尝试添加以下代码来代替返回行:

OracleQuery<String> parallelFullQuery = new OracleQuery<String>(conn)
.select(fullQuery)
.addFlag(QueryFlag.Position.START, "select /*+ parallel(2)*/ * from (")
.addFlag(QueryFlag.Position.END, ")")
.from(fullQuery);

return parallelFullQuery.fetch();

但是它覆盖了WITH子句。
有什么想法吗?
谢谢你。

dly7yett

dly7yett1#

您可以通过提供查询注解对定制的Hibernate Dialect执行此操作,可以通过JPA查询提示org.hibernate.comment设置注解,然后在Dialect中通过实现addSqlHintOrComment应用该注解

相关问题