コンポジット・キーを含む表の作成

Oracle NoSQL Database SDK for Spring Dataを使用して、コンポジット主キー・フィールドを含む表を作成する方法について学習します。

@NosqlKey注釈を使用して、注釈付きフィールドをコンポジット主キーのコンポーネントとして識別します。

例2-4 コンポジット主キー・フィールドを含む表の作成

次の例は、Studentをエンティティとしてモデル化し、universityIdacademicYearおよびstudentIdフィールドをコンポジット・キーとして使用する方法を示しています。

コンポジット・キーは、複数の主キー・フィールドを結合して一意の行を識別する場合に便利です。コンポジット・キー内では、シャード・キーの一部である主キー・フィールドを識別し、フィールドの順序を指定することもできます。

次の例は、学生データのキー・フィールドを含むコンポジット・キーを作成する方法を示しています。StudentKeyという名前のクラスをコンポジット・キー・クラスを表すように定義し、次の例で説明するようにStudentエンティティで使用します。

@NosqlKey注釈を使用してコンポジット・キー・クラスを作成し、コンポジット・キーを識別します。フィールドがシャード・キーの一部である場合は、shardKey値をtrueに設定します。すべてのフィールドのorder値を、表内の主キー・フィールドの生成順に設定します。shardKey要素およびorder要素の詳細は、表1-4を参照してください。

次のコード・サンプルでは、universityIdacademicYearおよびstudentIdフィールドは、学生のデータを識別するためのキー・フィールドを表し、@NosqlKey注釈を使用して主キー・フィールドとして宣言されます。主キー・フィールド内での順序付けの図では、2つの主キー・フィールドをシャード・キーとして、3番目のフィールドを非シャード・キーとして考えます。

universityIdおよびacademicYearフィールドのshardKey値をtrueに設定し、studentIdフィールドをfalseに設定します。universityIdフィールドの順序値を0に設定して、最初の主キー・フィールドとしてuniversityIdフィールドを作成し、2番目の主キー・フィールドとしてacademicYearを1に設定します。studentIdフィールドは非シャード・キーであるため、その順序値はシャード・キーより大きい必要があります。studentIdフィールドのorder値を2に設定します。
import com.oracle.nosql.spring.data.core.mapping.NosqlKey;
import java.io.Serializable;
import java.util.Objects;
 
/* Define a composite Key class */

public class StudentKey implements Serializable {
    
    @NosqlKey(shardKey = true, order = 0)
    long universityId;

    @NosqlKey(shardKey = true, order = 1)
    int academicYear;

    @NosqlKey(shardKey = false, order = 2)
    long studentId;

    /* public or package protected constructor required when retrieving from database */
    public StudentKey() {
    }

    public StudentKey(long universityId, int academicYear, long studentId) {
        this.universityId = universityId;
        this.academicYear = academicYear;
        this.studentId = studentId;
    }

    public long getUniversityId() {
        return universityId;
    }

    public void setUniversityId(long universityId) {
        this.universityId = universityId;
    }

    public int getAcademicYear() {
        return academicYear;
    }

    public void setAcademicYear(int academicYear) {
        this.academicYear = academicYear;
    }

    public long getStudentId() {
        return studentId;
    }

    public void setStudentId(long studentId) {
        this.studentId = studentId;
    }

    /* Define equals method */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof StudentKey)) {
            return false;
        }
        StudentKey studentKey = (StudentKey) o;
        return Objects.equals(universityId, studentKey.universityId) &&
                Objects.equals(academicYear, studentKey.academicYear) &&
                Objects.equals(studentId, studentKey.studentId);
    }

    /* Define hashcode method */
    @Override
    public int hashCode() {
        return Objects.hash(universityId, academicYear, studentId);
    }
}

コンポジット主キーとしてStudentKeyを持つStudentエンティティ・クラスを作成します。エンティティ・クラスのStudentKeyには、主キーを示すために@NosqlIdという注釈が付けられます。

エンティティ・クラスでは、キー以外のフィールドを宣言できます。キー以外のフィールドは、JSONデータとして kv_json_列に含まれます。
import com.oracle.nosql.spring.data.core.mapping.NosqlId;
import com.oracle.nosql.spring.data.core.mapping.NosqlTable;
import com.oracle.nosql.spring.data.core.mapping.NosqlKey;

import java.io.Serializable;
import java.util.Objects;
 
/*The @NosqlTable annotation specifies that
  this class will be mapped to an Oracle NoSQL Database table.*/

@NosqlTable

public class Student {
    @NosqlId 
    StudentKey studentKey;
    String firstName;
    String lastName;
    String resident;
    
    /* public or package protected constructor required when retrieving from database */
    public Student() {
        studentKey = new StudentKey();
    }
    
    
    /*This method overrides the toString() method, and then concatenates id and name, and then returns a String*/
    @Override
    public String toString() {
        return "Student{" +
                "universityId=" + studentKey.universityId + ", " +
                "academicYear=" + studentKey.academicYear + ", " +
                "studentId=" + studentKey.studentId + ", " +
                "firstName=" + firstName + ", " +
                "lastName=" + lastName + ", " +
                "resident=" + resident+
                '}';
    }
}

ノート:

NosqlDbConfig Spring Beanを提供するAppConfigクラスを設定する必要があります。NosqlDbConfig Spring Beanは、Oracle NoSQL Databaseへの接続方法を示します。また、NosqlRepositoryインタフェースを拡張するインタフェースを作成して、Oracle NoSQL Databaseからデータを取得する必要があります。詳細は、「Spring Data Frameworkを使用したOracle NoSQL Databaseへのアクセス」の項を参照してください。
Spring Data Frameworkは、次のDDLを使用してStudent表を作成します。
/* Student table DDL */

CREATE TABLE IF NOT EXISTS Student (
   universityId LONG,
   academicYear INTEGER,
   studentId LONG,
   kv_json_ JSON,
   PRIMARY KEY(SHARD(universityId, academicYear), studentId)
)

主キー・フィールドuniversityIdおよびacademicYearもシャード・キーで、studentIdはシャード以外の主キー・フィールドです。