Dynamic query, postgres

59 views
Skip to first unread message

i...@gmx.at

unread,
Sep 14, 2016, 7:05:56 AM9/14/16
to Querydsl
Hi,

just wanted to evaluate querydsl dynamic queries in combination with postgres.

Problem is, creating the correct SQLQueryFactory or "How do i execute the query".

Here is my code:


01. SQLTemplates templates = new PostgresTemplates();
02. Configuration config = new Configuration(templates);
03. PathBuilder<Object> myEntityPath = new PathBuilder<Object>(Object.class,"myentity");
04. NumberPath<Integer> idPath = myEntityPath.getNumber("id",Integer.class);
05. //Constant<String> idVal = Expressions.constant((T) 2);
06. //BooleanExpression be = Expressions.predicate(Ops.GT, idPath, idVal);
07. SQLQueryFactory f = new SQLServerQueryFactory(() -> c); // WHERE IS THE POSTGRES-Factory?
08. FilteredClause fi = f
09. .from(myEntityPath)
10. .where(idPath.gt(3))
11. ;
12.
13. // HOW TO EXECUTE THE QUERY?
14.



How can I execute a dynamic SQL query against a postgres database?
The documentation says I have to create an instance of "SQLQueryFactory" via the "new" keyword, but SQLQueryFactory is an interface.

Please, can anybody help me?

Thanks in advance,
Alex

google...@sql-workbench.net

unread,
Sep 15, 2016, 2:06:49 AM9/15/16
to Querydsl
You don't need a special query factory, the default one will work just fine with Postgres.


final Connection pgConn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", "user", "password");

Provider<Connection> provider = () -> pgConn;

SQLTemplates template = new PostgreSQLTemplates();
Configuration config = new Configuration(template);
SQLQueryFactory factory = new SQLQueryFactory(config, provider);


PathBuilder<Object> myEntityPath = new PathBuilder<Object>(Object.class,"myentity");
NumberPath<Integer> idPath = myEntityPath.getNumber("id",Integer.class);
SQLQuery<Object[]> query = factory.
   
select(SQLExpressions.all).
   
from(myEntityPath).
   
where(idPath.gt(3));
List<Object[]> fetch = query.fetch();

i...@gmx.at

unread,
Sep 15, 2016, 3:14:22 AM9/15/16
to Querydsl
Thanks for the quick response!
Ok. IntelliJ just did not list all version of querydsl - the max version that was schon is 2.1.13 or something like that.
Adding it by manually setting the current version nummber gives me the SQLQueryFactory that I can instantiate.

But I've another problem, now.
The factory does not have a "select" meethod. PLease see the following screenhot.
Do I have to add an additional library?


google...@sql-workbench.net

unread,
Sep 15, 2016, 3:27:36 AM9/15/16
to Querydsl
You are using the wrong version.

The Maven groupid is com.querydsl

So you need to use:

<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-sql</artifactId>
  <version>4.1.3</version>
</dependency>

See the manual for details: http://www.querydsl.com/static/querydsl/4.1.3/reference/html_single/#d0e716

i...@gmx.at

unread,
Sep 15, 2016, 5:43:39 AM9/15/16
to Querydsl
Thank you very much!
Now, with the correct package, it runs fine :)

Thanks and have a nice day!
Alex
Reply all
Reply to author
Forward
0 new messages