1

Are there any good utils/frameworks which could generate Java Object for SQL Query?

3 Answers 3

2

QueryDsl automatically creates Query Objects from your Hibernate, JPA or JDO classes, but also from your DB schema.

Querying with Querydsl SQL is as simple as this :

QCustomer customer = new QCustomer("c");

SQLTemplates dialect = new HSQLDBTemplates(); // SQL-dialect
SQLQuery query = new SQLQueryImpl(connection, dialect); 
List<String> lastNames = query.from(customer)
    .where(customer.firstName.eq("Bob"))
    .list(customer.lastName);

It also supports subqueries:

To create a subquery you create a SQLSubQuery instance, define the query parameters via from, where etc and use unique or list to create a subquery, which is just a type-safe Querydsl expression for the query. unique is used for a unique (single) result and list for a list result.

query.from(customer).where(
  customer.status.eq(
      new SQLSubQuery().from(customer2).unique(customer2.status.max()))
  .list(customer.all())  

Another example

query.from(customer).where(  
  customer.status.in(new SQLSubQuery().from(status).where(
    status.level.lt(3)).list(status.id))  
  .list(customer.all())    
1

I don't know its gonna be enough helpful but, as you asked for utils, I would suggest you to read about the QUERY OBJECT PATTERN (P of EAA, M. Fowler), if you have time to implement something, its a good beginning, otherwise you may lookfor any ORM framework.

1
  • Are you aware of any frameworks, which could help in designing subQueries and joins too? For ex: Select * from person where id = (select * from map where id=223)
    – Phani
    Mar 23, 2011 at 5:35
0

I am using torque to do that. There is an example(Tutorial) which show what it can do at http://db.apache.org/torque/releases/torque-3.3/tutorial/step5.html

But what exactly is it you want? Do you just a simple way to serialize/unserialize objects to the database, and load them based on a primary/foreign key, or do you need to issue really complicated queries?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.