Persistence Model
An entity is a lightweight persistence domain object. The persistent state of an entity is represented through persistent fields using Java Beans / Plain Old Java Objects (POJOs).
The Spring Data Framework supports the persistence of entities to Oracle NoSQL Database tables. An entity is mapped to a table. The ID field in that entity is mapped to the primary key column of that table. All other fields in the entity are mapped to a JSON column of that table. Each instance of the entity will be stored as a single row in that table. The value of the ID field in that instance will be stored as the primary key value of that row. The values of all other fields (including other objects) (see JSON Column) in that instance will be serialized and stored as values in the JSON column of that row. Effectively, the table will always have only two columns: a primary key column and a JSON column.
                  
If a persistent POJO has a reference to another persistent POJO (nested objects) that maps to a different table, the Spring Data Framework will not serialize objects to multiple tables. Instead, all the nested objects will be serialized and stored as values in the JSON column. For more information on JSON Column mappings, see JSON Column.
The following is the syntax of an entity with @NosqlTable and @NosqlId annotations. In the example below, the Student class with the @NosqlTable annotation will be mapped to a table named Student in the Oracle NoSQL Database. The ID field with the @NosqlId annotation will be the primary key field in the Student table. The firstName and lastName fields will be mapped to a single JSON field named kv_json_ in the Student table.
                  
Note:
The classes may have other constructors too./*The @NosqlTable annotation specifies that 
this class will be mapped to an Oracle NoSQL Database table.*/
@NosqlTable
public class Student {
    //The @NosqlId annotation specifies that this field will act as the ID field.
    @NosqlId
    public long ID;
  
    public String firstName;
    public String lastName;
  
    public Student() {}
}Table Name
By default, the entity simple class name is used for the table name. You can provide a different table name using the @NosqlTable annotation. The @NosqlTable annotation enables you to define additional configuration parameters such as table name and timeout.
                  
For example, an entity named Student will be persisted in a table named Student. If you want to persist an entity named Student in a table named Learner, you can achieve that using the @NosqlTable annotation.
                  
If the @NosqlTable annotation is specified, then the following configuration could be provided.
                  
Table 1-2 Attributes in NosqlTable Annotation
| Parameter | Type | Ignored/ Optional/ Required in Oracle NoSQL Database | Ingnored/ Optional/ Required in Oracle NoSQL Database Cloud Service | Default | Description | 
|---|---|---|---|---|---|
| tableName | String | Optional | Optional | empty | Specifies the name of the table, simple or namespace-qualified form. If empty, then the entity class name will be used. For more information on the namespace, see Namespace Management in the SQL Reference Guide. In the Oracle NoSQL Database Cloud Service, the namespace part, if provided, is used as the compartment name. For more information on using compartments, see Creating a Compartment in the Oracle NoSQL Database Cloud Service Guide. | 
| autoCreateTable | boolean | Optional | Optional | true | Specifies if the table should be created if it does not exist. Note:The Spring Data Framework looks for the repositories used in the application in the initphase. If the table does not exist,
                                    and if the@NosqlTableannotation has theautoCreateTableastrue,
                                    then the table will be created in theinitphase. | 
| readUnits | int | Ignored | Required | -1 | Specifies the maximum read throughput to be used if the table is to be created. For more information on  Note:In Oracle NoSQL Database Cloud Service, the readUnitsparameter should be set to a value greater than0else it will return an error. | 
| writeUnits | int | Ignored | Required | -1 | Specifies the maximum write throughput to be used if the table is to be created. For more information on  Note:In Oracle NoSQL Database Cloud Service, the writeUnitsparameter should be set to a value greater than0else it will return an error. | 
| storageGB | int | Ingored | Required | -1 | Specifies the maximum amount of storage, in gigabytes, allowed for the table, if the table is to be created. For more information on  Note:In Oracle NoSQL Database Cloud Service, the storageGBparameter should be set to a value greater than0else it will return an error. | 
| timeout | int | Optional | Optional | 0 | Specifies the maximum time length, in milliseconds, that the operations are allowed to take before a timeout exception is thrown. If the value for  The  | 
| consistency | String | Optional | Optional | EVENTUAL | Specifies the consistency used for read operations. Valid values are based on  Note:This is the default for all read operations. It can be overridden by using NosqlRepository.setConsistency(String). For more information, see setConsistency in the SDK for Spring Data API Reference. | 
| durability | String | Optional | Optional | COMMIT_NO_SYNC | Sets the default durability for all the write operations applied to this table. Valid values based on  | 
| capacityMode | NosqlCapacityMode For more information, see NosqlCapacityMode. | Optional | Optional | NosqlCapacityMode.PROVISIONED | Sets the capacity mode when the table is created. This applies only in cloud or cloud sim scenarios. A table is created with either  Set the values for the TableLimits instance based on the capacity mode as follows: 
                                     
 | 
| ttl | int | Optional | Optional | 0 | Sets the default table level Time to Live (TTL) when the table is created. The TTL allows the automatic expiration of table rows after the elapse of the specified duration. If the value is not set, the value  This parameter is applicable only when autoCreateTable is set to true. | 
| ttlUnit | TtlUnit | Optional | Optional | NosqlTable.TtlUnit.DAYS | Sets the unit of TTL value. The valid values are:  If the value is not set, the default value of days is used. This parameter is applicable only when autoCreateTable is set to true. | 
Primary Key
The table requires a primary key. The field named ID in the entity will be used as the primary key. You can select a different field in the entity (a field with a different name other than ID) to designate as the primary key using the @NosqlId annotation or the @id annotation.
                  
When an ID field is mapped to a primary key column, the Spring Data Framework will automatically assign the corresponding data type to the ID field before storing it in the table. The following is a list of data type mappings between a Java type and an Oracle NoSQL Database type for the ID field.
                  
The Java types that are provided in the following table are the only valid data types that can be used for a primary key.
Table 1-3 Mapping Between Java and Oracle NoSQL Database Types
| Java Type | Oracle NoSQL Database Type | 
|---|---|
| 
 | 
 | 
| 
 
 | 
 | 
| 
 
 | 
 | 
| 
 
 
 
 | 
 Note: double,java.lang.Double,float, andjava.lang.Floatcan be a primary key but it's not a validgenerated=truetypeNote:Since FLOATin Oracle NoSQL Database type is not explicitly used in NoSQL SDK for Java, the Javafloatandjava.lang.Floatare mapped to theDOUBLEtype. | 
| 
 
 | 
 | 
| 
 
 | 
 | 
| 
 
 
 | 
 | 
- @NosqlIdannotation: If- @NosqlIdannotation is used on a field with a valid data type for the primary key, then that field is considered the primary key. If- @NosqlIdis used on a field of a type other than a valid data type for the primary key, an error is raised. For more information, see NosqlId in the SDK for Spring Data API Reference.
- @org.springframework.data.annotation.Idannotation: If- @org.springframework.data.annotation.Idfield annotation is used on a field with a valid data type for the primary key, then that field is considered as the primary key. If- @org.springframework.data.annotation.Idis used on a field of a type other than a valid data type for the primary key, an error is raised.
- Not specified: If none of the above two annotations are specified, then the Spring Data Framework will use the field named IDas the primary key.
- No @NosqlIdannotation or@org.springframework.data.annotation.Idannotation orIDfield is found in the entity, as no primary key field can be inferred.
- Two or more of the @NosqlIdor@org.springframework.data.annotation.Idannotated fields are used in the entity, as multiple primary key fields can be inferred.
Note:
The name of the fields that take the@NosqlId or @org.springframework.data.annotation.Id annotations must not be named kv_json_. This is because the data column of the table created by the Spring Data Framework will be named kv_json_ and will be a JSON column where all attributes in the persistent entity that are not listed as primary key attributes will be stored.
                  The @NosqlId field annotation can take the following additional configuration:
                  
Table 1-4 Attributes in NosqlId Annotation
| Parameter | Type | Optional/Required | Default | Description | 
|---|---|---|---|---|
| generated | boolean | Optional | false | Specifies if the  
 Note:You can't auto-generate composite keys. Setting @NosqlId.autoGenerated=trueleads to an error. You must manage the key values for all read/write calls when using the composite keys. If the key values are not set, the Oracle NoSQL Database generates an error. | 
Composite Primary Keys
Composite primary keys contain more than one primary key field. You can define a composite key class type to represent the composite keys.
A composite key class is a type that is mapped to multiple primary key fields of the entity class. A composite key class must be serializable and must define equals and hashcode methods. This class must consist of fields that are primitive data types.
Note:
The equality checks for the user-defined methods in the composite key class must be consistent with the equality checks performed in the Oracle NoSQL Database between the database types and their mapped keys.You can use @NosqlKey annotation to specify the components of a composite primary key in the composite key class. 
                  
Table 1-5 Attributes in the Nosqlkey Annotation
| Parameter | Type | Optional/Required | Default | Description | 
|---|---|---|---|---|
| shardKey | boolean | Optional | true | Identifies if a primary key field is also a Shard Key. Shard keys affect the distribution of rows across shards. 
 If you do not  supply the  | 
| order | int | Optional | System determined | Specifies the ordering of the shard keys and non-shard keys within the primary key in a composite key class. You can set the  
 If you do not specify the  | 
For more details on @NosqlKey annotation, see NosqlKey in the SDK for Spring Data API Reference document.
                  
Example: Ordering the composite keys
Consider primary key fields universityId, academicYear, and studentId defined in a composite key class.
universityId and academicYear fields to be a part of the shard key. The order values of these shard keys must be lesser than the studentId field, which is a non-shard key. You can use the following sample code to create a composite class./* Define a composite Key class */
public class StudentKey implements Serializable {
    
    @NosqlKey(shardKey = true, order = 1)
    long universityId;
    @NosqlKey(shardKey = true, order = 0)
    int academicYear;
    @NosqlKey(shardKey = false, order = 2)
    long studentId;
    /* public or package protected constructor required when retrieving from database */
    public StudentKey() {
    }
}In the example above, the academicYear field is considered as the first primary key field during the creation of the table. 
                     
/* Table DDL */
CREATE TABLE IF NOT EXISTS Students (
   academicYear INTEGER,
   universityId LONG,
   studentId LONG,
   kv_json_ JSON,
   PRIMARY KEY(SHARD(academicYear, universityId), studentId)
)order field. /* Define a composite Key class */
public class StudentKey implements Serializable {
    
    @NosqlKey(shardKey = true)
    long universityId;
    @NosqlKey(shardKey = true)
    int academicYear;
    @NosqlKey(shardKey = false)
    long studentId;
    @NosqlKey(shardKey = false)
    long branchId;
    /* public or package protected constructor required when retrieving from database */
    public StudentKey() {
    }
}/* Table DDL */
CREATE TABLE IF NOT EXISTS Students (
   academicYear INTEGER,
   universityId LONG,
   branchId LONG,
   studentId LONG,
   kv_json_ JSON,
   PRIMARY KEY(SHARD(academicYear, universityId), branchId, studentId)
)In the following cases, the Spring Data Framework considers all the primary key fields as shard keys and uses alphabetical ordering:
- If you declare the primary key fields in the composite key class without using the @NosqlKeyannotation.
- If you declare the primary key fields in the composite key class without specifying the shardKeyand theordervalues in the@NosqlKeyannotation.
Note the following properties of the composite key class.
- You must have at least one field with shardKey=truein the composite key class, otherwise, the Spring Data Framework will generate an error.
- You can use a composite key class with repositories (as the IDtype) and to represent an entity’s identity in a single object.
- You can annotate the fields as @transientto designate the non-persistent state of the field.
- You can't nest composite key classes. This will generate an error.
- You can't auto-generate composite primary key fields. Setting @NosqlId.autoGenerated=trueleads to an error. You must manage the key values for all read/write calls when using the composite keys. If the key values are not set, the Oracle NoSQL Database generates an error.
JSON Column
All other fields in the entity other than the primary key field will be converted into a NoSQL JSON value with the following rules:
- The Java scalar values will be converted to NoSQL JSON atomic values.
- The Java collections and array structures will be converted to a NoSQL JSON array.
- The Java non-scalar values will be recursively converted to NoSQL JSON objects.
- The Java null values will be converted to NoSQL JSON NULL values.
- The complex values will be converted to NoSQL JSON objects according to the following table.
Table 1-6 Mapping Between Java and NoSQL JSON Types
| Java Type | Representation within Oracle NoSQL Database JSON Datatype | 
|---|---|
| 
 | 
 | 
| 
 
 | 
 | 
| 
 
 | 
 | 
| 
 
 
 
 | 
 Note:Since FLOATin Oracle NoSQL Database type is not explicitly used in NoSQL SDK for Java, Javafloat, andjava.lang.Floatare mapped to theDOUBLEtype. | 
| 
 
 | 
 | 
| 
 
 | 
 | 
| 
 | 
 | 
| 
 
 
 | 
 | 
| 
 | GeoJson Point For more information on GeoJson Data, see About GeoJson Data in the SQL Reference Guide. | 
| 
 | GeoJson Polygon For more information on GeoJson Data, see About GeoJson Data in the SQL Reference Guide . Note:Polygons must conform to the following rules to be well-formed, otherwise they will be ignored when used in queries. 
 Before inserting new polygons in the table, the
                                             | 
| 
 
 
 
 
 
 
 
 
 
 
 | 
 Note: 
 | 
| 
 | 
 | 
| 
 | 
 | 
| 
 
 
 
 
 
 
 | MAP(JSON) Note: 
 | 
Note:
Java data structures that contain cycles are neither supported nor detected. That is, if the entity object is traversed from the root down the fields and encounters the same object twice it becomes a cycle.