Reputation: 1795
I would like to build select count query by using QueryDSL library, something like this:
select count(1) from table1
I created next code:
SQLTemplates sqlTemplates = builder.printSchema().build();
Configuration configuration = new Configuration(sqlTemplates);
Path table = new RelationalPathBase(Object.class, "alias", "hr", "table1");
StringPath[] columnsPath = new StringPath[1];
columnsPath[0] = Expressions.stringPath(table, "field1");
SQLQuery sqlQuery = new SQLQuery(dataSource.getConnection(), configuration)
.from(table);
sqlQuery.setUseLiterals(true);
String selectStatement = sqlQuery.getSQL(columnsPath).getSQL();
As result selectStatement is next:
select alias.field1 from hr.table1 alias
Could please someone advice how to rewrite code above to get
select count(alias.field1) from hr.table1 alias
Upvotes: 5
Views: 14627
Reputation: 22190
Replace the last line with
String selectStatement = sqlQuery.getSQL(columnsPath[0].count()).getSQL();
Upvotes: 4