5

データベースに依存しない方法でクエリを作成する Java ライブラリを探しているときに、iciql、querydsl、jooq、joist、hibernate などを含む多くのライブラリに出会いました。

構成ファイルを必要とせず、動的スキーマで動作するものが必要でした。私のアプリケーションでは、実行時にデータベースとスキーマについて知るようになるので、スキーマ用の構成ファイルやドメイン クラスはありません。

これは querydsl の中心的な目標の 1 つと思われますが、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 でそのようなことが可能かどうか、可能であればその方法を教えてください。

私の要件を満たすことができる他のものを誰かが知っていれば、それは本当にありがたいです.

4

3 に答える 3

6

次のような非常に単純な 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));
}
于 2014-02-07T14:59:12.527 に答える
4

これは、PathBuilder を使用したポンザオのソリューションの小さなバリエーションです。

@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));
}
于 2014-02-07T19:12:42.043 に答える