エンティティ状態検出戦略

次の表は、エンティティが新しいかどうかを検出するために Spring Data が提供する戦略を説明しています。

表 1: Spring Data でエンティティが新しいかどうかを検出するためのオプション

@Id-Property inspection (the default)

By default, Spring Data inspects the identifier property of the given entity. If the identifier property is null or 0 in case of primitive types, then the entity is assumed to be new. Otherwise, it is assumed to not be new.

@Version-Property inspection

If a property annotated with @Version is present and null, or in case of a version property of primitive type 0 the entity is considered new. If the version property is present but has a different value, the entity is considered to not be new. If no version property is present Spring Data falls back to inspection of the identifier property.

Implementing Persistable

エンティティが Persistable を実装している場合、Spring Data はエンティティの isNew(…) メソッドに新しい検出を委譲します。詳細については、Javadoc を参照してください。

注: AccessType.PROPERTY を使用すると、Persistable のプロパティが検出されて保持されます。これを回避するには、@Transient を使用します

カスタム EntityInformation 実装の提供

モジュール固有のリポジトリファクトリのサブクラスを作成し、getEntityInformation(…) メソッドをオーバーライドすることにより、リポジトリベースの実装で使用される EntityInformation 抽象化をカスタマイズできます。次に、モジュール固有のリポジトリファクトリのカスタム実装を Spring Bean として登録する必要があります。これが必要になることはめったにないことに注意してください。