0

sample_clause

The sample_clause lets you instruct the database to select from a random sample of data from the table, rather than from the entire table.

I want to run below query using QueryDSL

sample_clause

The sample_clause lets you instruct the database to select from a random sample of data from the table, rather than from the entire table.

select from Test t SAMPLE(80) WHERE t.test_id=01 and t.test_suite_id=02;

where the condition is dynamic and I am generating it using queryDSL however I don't know how to add the SAMPLE keyword to query DSL.

public Long getCount(TestDTO testDTO) {
    JPAQuery<Tuple> query = new JPAQuery<>(entityManager);
    QTest qTest=QTest.test;
    //dynamic where condition.
    OptionalBooleanBuilder where = buildCondition(testDTO);
    
    List<BigDecimal> output=query
            .select(qTest.testId)
            .from(qTest)
            .where(where.build()).fetch();
   //finally return the output.
}

1 Answers1

0

SAMPLE is a database dialect specific function that is not available in JPQL, the query language of JPA. As such, you need to register a custom function for your dialect with your ORM implementation. You could register these functions yourself using custom functions.

However, after this, these functions still won't be accessible in QueryDSL. In order to do so, you'd have to implement a custom Operation implementation and associate it with a "template" in a subclass of JPQLTemplates. Alternatively, you could create a TemplateExpression inline: Expressions.template(Integer.class, "SAMPLE({0})", arg1).