コンポジット・キーを含む表の作成
Oracle NoSQL Database SDK for Spring Dataを使用して、コンポジット主キー・フィールドを含む表を作成する方法について学習します。
@NosqlKey
注釈を使用して、注釈付きフィールドをコンポジット主キーのコンポーネントとして識別します。
例2-4 コンポジット主キー・フィールドを含む表の作成
次の例は、Student
をエンティティとしてモデル化し、universityId、academicYearおよびstudentIdフィールドをコンポジット・キーとして使用する方法を示しています。
コンポジット・キーは、複数の主キー・フィールドを結合して一意の行を識別する場合に便利です。コンポジット・キー内では、シャード・キーの一部である主キー・フィールドを識別し、フィールドの順序を指定することもできます。
次の例は、学生データのキー・フィールドを含むコンポジット・キーを作成する方法を示しています。StudentKey
という名前のクラスをコンポジット・キー・クラスを表すように定義し、次の例で説明するようにStudent
エンティティで使用します。
@NosqlKey
注釈を使用してコンポジット・キー・クラスを作成し、コンポジット・キーを識別します。フィールドがシャード・キーの一部である場合は、shardKey
値をtrue
に設定します。すべてのフィールドのorder
値を、表内の主キー・フィールドの生成順に設定します。shardKey
要素およびorder
要素の詳細は、表1-4を参照してください。
次のコード・サンプルでは、universityId
、academicYear
および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
という注釈が付けられます。
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へのアクセス」の項を参照してください。
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
はシャード以外の主キー・フィールドです。