リポジトリの定義
永続性のために使用するエンティティ・クラスは、注釈または継承によってSpring Data Frameworkで検出できます。NosqlRepository
インタフェースを使用すると、永続性のためにOracle NoSQL Databaseを使用するエンティティごとにインタフェースを継承して作成できます。
NosqlRepository
インタフェースでは、問合せを定義する多数のメソッドを備えたSpringのPagingAndSortingRepository
インタフェースを拡張します。
NosqlRepository
インタフェースに備わっているメソッドに加えて、導出問合せを定義するためにリポジトリ・インタフェースにメソッドを追加できます。こうしたインタフェース・メソッドは、Spring Data Frameworkによって捕捉されるSpringの導出問合せの特定のネーミング・パターンに従います(導出問合せの詳細は、「Query Creation」を参照)。Spring Data Frameworkは、このネーミング・パターンを使用して式ツリーを生成し、このツリーをOracle NoSQL Database SDK for Spring Dataに渡します。そこで、この式ツリーがOracle NoSQL Database問合せに変換されます。その問合せがコンパイルされてから実行されます。こうしたOracle NoSQL Database問合せは、リポジトリの各メソッドをコールすると実行されます。
導出問合せを作成する場合は、NosqlRepository
インタフェースを拡張し、導出問合せの項で説明するようにネーミング・パターンに準拠する独自のJavaメソッド・シグネチャを追加する必要があります。
次に、NosqlRepository
インタフェースを実装するコードの例を示します。バインドされた型パラメータ(ID
フィールドのエンティティ型とデータ型)を指定する必要があります。このインタフェースは、導出問合せfindByLastName
を実装して、Student
クラスの反復可能なインスタンスを返します。
import com.oracle.nosql.spring.data.repository.NosqlRepository;
/*The Student is the entity class, and Long is the data type of the
primary key in the Student class. This interface implements a derived query
findByLastName and returns an iterable instance of the Student class.*/
public interface StudentRepository extends NosqlRepository<Student, Long> {
/*The Student is searched by lastname and
an iterable instance of the Student class is returned.*/
Iterable<Student> findByLastName(String lastname);
}