开发者

Designing Java Object for SQL Query

Are the开发者_StackOverflow中文版re any good utils/frameworks which could generate Java Object for SQL Query?


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())    


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜