如何在没有域类的情况下在querydsl中构造查询

时间:2014-02-06 23:00:26

标签: java sql jooq querydsl database-agnostic

在寻找java库以数据库无关的方式构建查询时,我遇到了很多,包括iciql,querydsl,jooq,joist,hibernate等。

我想要的东西不需要配置文件,可以使用动态模式。 对于我的应用程序,我在运行时了解数据库和模式,因此我不会为模式提供任何配置文件或域类。

这似乎是querydsl的核心目标之一,但是通过querydsl的文档我看到了许多使用域类构建动态查询的示例,但我没有遇到任何解释如何构建此类数据库不可知查询的内容仅使用我对模式的动态信息。

Jooq提供此类功能(请参阅:http://www.jooq.org/doc/3.2/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder/)但如果我想将重点扩展到Oracle或MS SQL(我可能不喜欢但需要支持),则会有限制性许可。

具有querydsl经验的人是否可以通过querydsl让我知道是否可以做到这一点,如果是,那该怎么做。

如果有人知道其他任何可以满足我要求的人,我们将非常感激。

3 个答案:

答案 0 :(得分:3)

一个非常简单的SQL查询,例如:

@Transactional
public User findById(Long id) {
    return new SQLQuery(getConnection(), getConfiguration())
      .from(user)
      .where(user.id.eq(id))
      .singleResult(user);
}

...可以像这样动态创建(不添加任何糖):

@Transactional
public User findById(Long id) {
    Path<Object> userPath = new PathImpl<Object>(Object.class, "user");
    NumberPath<Long> idPath = Expressions.numberPath(Long.class, userPath, "id");
    StringPath usernamePath = Expressions.stringPath(userPath, "username");
    Tuple tuple = new SQLQuery(getConnection(), getConfiguration())
      .from(userPath)
      .where(idPath.eq(id))
      .singleResult(idPath, usernamePath);
    return new User(tuple.get(idPath), tuple.get(usernamePath));
}

答案 1 :(得分:3)

以下是使用PathBuilder的ponzao解决方案的一小部分

@Transactional
public User findById(Long id) {        
    PathBuilder<Object> userPath = new PathBuilder<Object>(Object.class, "user");
    NumberPath<Long> idPath = userPath.getNumber("id", Long.class);
    StringPath usernamePath = userPath.getString("username");
    Tuple tuple = new SQLQuery(getConnection(), getConfiguration())
      .from(userPath)
      .where(idPath.eq(id))
      .singleResult(idPath, usernamePath);
    return new User(tuple.get(idPath), tuple.get(usernamePath));
}

答案 2 :(得分:1)

更新:Timo通过向我展示如何做我想要的而不必替换SQLQuery类来使我的原始响应无效。以下是他的评论:

query.getSQL(field1, field2, ... fieldN), getSQL is consistent with the
other methods which also take the projection arguments at last

我删除了不必要的课程。下面是一个使用SQLQuery直接获取SQL字符串而不执行查询的示例(例如,不使用list方法):

SQLQuery rquery = new SQLQuery(connection , dialect);

// Use getSQL with projections
rquery.from(qtable)
    .where(qtable.qfield1.eq("somevalue"));

SQLBindings bindings = rquery.getSQL(qtable.qfield1, qtable.qfield2);

// Get the SQL string from the SQLBindings
System.out.println(bindings.getSql());

// Get the SQL parameters from the SQLBindings for the parameterized query
System.out.println(bindings.getBindings());

此响应回答了如何使用QueryDSL构建完整的SQL查询而不实际执行查询。它没有解决有关“动态模式”和“没有域对象”的其他要求......