Skip to content

Commit

Permalink
Enabled mapping of query execution results to any specified entity
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamura-to committed Nov 22, 2023
1 parent d869297 commit bd1a247
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions content/en/docs/Reference/Query/QueryDsl/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ select t0_.STREET from ADDRESS as t0_ where t0_.ADDRESS_ID in (?, ?) order by t0
*/
```

## selectAsEntity

If you want to project the result and receive it as a specific entity, call `selectAsEntity`. Specify the entity's metamodel as the first argument, and the properties to project as the subsequent arguments. The order and type of properties must match the constructor of the entity class.

In the following example, the `EMPLOYEE` table is being queried, but the result is received as an `Address` entity.

```kotlin
val query: Query<List<Address>> = QueryDsl.from(e)
.selectAsEntity(a, e.addressId, e.employeeName, e.version)
/*
select t0_.ADDRESS_ID, t0_.EMPLOYEE_NAME, t0_.VERSION from EMPLOYEE as t0_
*/
```

## having

To specify a HAVING clause, call the `having` function:
Expand Down
15 changes: 15 additions & 0 deletions content/en/docs/Reference/Query/QueryDsl/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ The `select` function converts a `Row` to any type using the given lambda expres
It has functions to retrieve values by column label or index.
Note that the index starts from 0.

### selectAsEntity

If you want to receive the result as a specific entity, call `selectAsEntity`.
For the first argument, specify the entity's metamodel.
The SELECT clause in the SQL template must include columns corresponding to all properties of the entity.

In the following example, the result is received as an `Address` entity.

```kotlin
val sql = "select address_id, street, version from ADDRESS where street = /*street*/'test'"
val query: Query<List<Address>> = QueryDsl.fromTemplate(sql)
.bind("street", "STREET 10")
.selectAsEntity(a)
```

### options {#select-options}

To customize the behavior of the query, call the `options` function.
Expand Down
16 changes: 16 additions & 0 deletions content/ja/docs/Reference/Query/QueryDsl/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ select t0_.STREET from ADDRESS as t0_ where t0_.ADDRESS_ID in (?, ?) order by t0
*/
```

## selectAsEntity

結果を射影して任意のエンティティとして受け取りたい場合は`selectAsEntity`を呼び出します。
第一引数にはエンティティのメタモデル、第二引数以降には射影するプロパティを指定します。
プロパティの順番や型はエンティティクラスのコンストラクタに合わせなければいけません。

次の例では`EMPLOYEE`テーブルを検索していますが、結果を`Address`エンティティとして受け取っています。

```kotlin
val query: Query<List<Address> = QueryDsl.from(e)
.selectAsEntity(a, e.addressId, e.employeeName, e.version)
/*
select t0_.ADDRESS_ID, t0_.EMPLOYEE_NAME, t0_.VERSION from EMPLOYEE as t0_
*/
```

## having

HAVING句を指定するには`having`を呼び出します。
Expand Down
15 changes: 15 additions & 0 deletions content/ja/docs/Reference/Query/QueryDsl/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ val query: Query<List<Address>> = QueryDsl.fromTemplate(sql)
カラムのラベル名やインデックスで値を取得する関数を持ちます。
なお、インデックスは0から始まります。

### selectAsEntity

結果を任意のエンティティとして受け取りたい場合は`selectAsEntity`を呼び出します。
第一引数にはエンティティのメタモデルを指定します。
SQLテンプレートのSELECT句にはエンティティの全プロパティに対応するカラムが含まれていなければいけません。

次の例では結果を`Address`エンティティとして受け取っています。

```kotlin
val sql = "select address_id, street, version from ADDRESS where street = /*street*/'test'"
val query: Query<List<Address>> = QueryDsl.fromTemplate(sql)
.bind("street", "STREET 10")
.selectAsEntity(a)
```

### options {#select-options}

クエリの挙動をカスタマイズするには`options`を呼び出します。
Expand Down

0 comments on commit bd1a247

Please sign in to comment.