

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 不変データクラスでの操作
<a name="ddb-en-client-use-immut"></a>

DynamoDB Enhanced Client API のマッピング機能は、不変データクラスで動作します。不変クラスにはゲッターしかなく、SDK がクラスのインスタンスを作成するために使用するビルダークラスが必要です。不変クラスは、[カスタマークラス](ddb-en-client-gs-tableschema.md#ddb-en-client-gs-tableschema-anno-bean-cust)に示されている `@DynamoDbBean` 注釈を使用する代わりに、使用するビルダークラスを示すパラメータを受け取る `@DynamoDbImmutable` 注釈を使用します。

次のクラスは `Customer` の不変バージョンです。

```
package org.example.tests.model.immutable;

import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbImmutable;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondaryPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondarySortKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;

import java.time.Instant;

@DynamoDbImmutable(builder = CustomerImmutable.Builder.class)
public class CustomerImmutable {
    private final String id;
    private final String name;
    private final String email;
    private final Instant regDate;

    private CustomerImmutable(Builder b) {
        this.id = b.id;
        this.email = b.email;
        this.name = b.name;
        this.regDate = b.regDate;
    }

    // This method will be automatically discovered and used by the TableSchema.
    public static Builder builder() { return new Builder(); }

    @DynamoDbPartitionKey
    public String id() { return this.id; }

    @DynamoDbSortKey
    public String email() { return this.email; }

    @DynamoDbSecondaryPartitionKey(indexNames = "customers_by_name")
    public String name() { return this.name; }

    @DynamoDbSecondarySortKey(indexNames = {"customers_by_date", "customers_by_name"})
    public Instant regDate() { return this.regDate; }

    public static final class Builder {
        private String id;
        private String email;
        private String name;
        private Instant regDate;

        // The private Builder constructor is visible to the enclosing CustomerImmutable class.
        private Builder() {}

        public Builder id(String id) { this.id = id; return this; }
        public Builder email(String email) { this.email = email; return this; }
        public Builder name(String name) { this.name = name; return this; }
        public Builder regDate(Instant regDate) { this.regDate = regDate; return this; }

        // This method will be automatically discovered and used by the TableSchema.
        public CustomerImmutable build() { return new CustomerImmutable(this); }
    }
}
```

データクラスに `@DynamoDbImmutable` 注釈を付けるには、次の要件を満たす必要があります。

1. `Object.class` のオーバーライドされておらず、`@DynamoDbIgnore` 注釈も付いていないすべてのメソッドは、DynamoDB テーブルの属性のゲッターでなければなりません。

1. すべてのゲッターには、ビルダークラスに対応する大文字と小文字を区別するセッターが必要です。

1. 次のコンストラクト条件のうち 1 つだけ満たす必要があります。
   + ビルダークラスにはパブリックデフォルトコンストラクタが必要です。
   + データクラスには、パラメータを取らずにビルダークラスのインスタンスを返す、`builder()` という名前のパブリック静的メソッドが必要です。このオプションは不変 `Customer` クラスに表示されます。

1.  ビルダークラスには、パラメータを取らずに不変クラスのインスタンスを返す、`build()` という名前のパブリックメソッドが必要です。

不変クラスの `TableSchema` を作成するには、次のスニペットに示すように `TableSchema` の `fromImmutableClass()` メソッドを使用します。

```
static final TableSchema<CustomerImmutable> customerImmutableTableSchema = 
                         TableSchema.fromImmutableClass(CustomerImmutable.class);
```

不変クラスから DynamoDB テーブルを作成できるのと同様に、次のスニペットの例に示すように、`DynamoDbTable` の `createTable()` を *1 回*呼び出すだけで不変クラスからテーブルを作成できます。

```
static void createTableFromImmutable(DynamoDbEnhancedClient enhancedClient, String tableName, DynamoDbWaiter waiter){
    // First, create an in-memory representation of the table using the 'table()' method of the DynamoDb Enhanced Client.
    // 'table()' accepts a name for the table and a TableSchema instance that you created previously.
    DynamoDbTable<CustomerImmutable> customerDynamoDbTable = enhancedClient
            .table(tableName, TableSchema.fromImmutableClass(CustomerImmutable.class));
        
    // Second, call the 'createTable()' method on the DynamoDbTable instance.
    customerDynamoDbTable.createTable();
    waiter.waitUntilTableExists(b -> b.tableName(tableName));
}
```

## Lombok などのサードパーティライブラリを使用します。
<a name="ddb-en-client-use-immut-lombok"></a>

[Project Lombok](https://projectlombok.org/) などのサードパーティライブラリは、不変オブジェクトに関連するボイラープレートコードを生成するのに役立ちます。DynamoDB Enhanced Client API は、データクラスがこのセクションで説明する規則に従っている限り、これらのライブラリで動作します。

次の例は、Lombok 注釈付きの不変 `CustomerImmutable` クラスを示しています。Lombok の `onMethod` 機能が、`@DynamoDbPartitionKey` のような属性ベースの DynamoDB 注釈を生成されたコードにコピーすることに注意してください。

```
@Value
@Builder
@DynamoDbImmutable(builder = Customer.CustomerBuilder.class)
public class Customer {
    @Getter(onMethod_=@DynamoDbPartitionKey)
    private String id;

    @Getter(onMethod_=@DynamoDbSortKey)
    private String email;

    @Getter(onMethod_=@DynamoDbSecondaryPartitionKey(indexNames = "customers_by_name"))
    private String name;

    @Getter(onMethod_=@DynamoDbSecondarySortKey(indexNames = {"customers_by_date", "customers_by_name"}))
    private Instant createdDate;
}
```