Using SpEl expressions in NosqlTable.tableName annotation

You can specify the name of the table by setting the tableName parameter in the @NosqlTable annotation. In the above Student class example, since the tableName is not explicitly provided, by default an empty value is set and the entity class name is used as the name of the table by the Spring driver.

Spring Expression Language (SpEl) is a way to evaluate complex expressions at run time. For more details, see Spring Expression Language.

The @NosqlTable.tableName parameter supports evaluating (SpEl) expressions. You can use the SpEL expressions while setting the tableName parameter in the @NosqlTable annotation as shown in the following examples. The expressions are evaluated dynamically at run time.

Table 1-1 Using SpEL Expressions

SpEL expression in the tableName parameter Description
@NosqlTable(tableName = "#{ systemProperties['sys_ns']}:Customer")

The Customer table is created in the namespace defined by JVM system property sys_ns. If the system property doesn't exist, the SpEl expression evaluates to empty string, in which case the table is created in the default namespace, sysdefault.

The systemProperties attribute is a predefined variable.

To run with the JVM system property use:
java -Dsys_ns=myCustomNamespace ...
@NosqlTable(tableName = "#{ @environment.getProperty('ENV_NS')}:Customer")

The Customer table is created in the namespace defined by the environment property ENV_NS. If the environment variable doesn't exist the table is created in the default namespace, sysdefault.

To run by setting environment property use:
ENV_NS=myCustomNamespace; java ...
@NosqlTable(tableName = "${app.ns}:Customer") The Customer table is created in the namespace defined by the app.ns property in application.properties resource file. An error is thrown if the property does not exist.
@NosqlTable(tableName = "${app.ns}:Customer") The Customer table is created in the namespace defined by the app.ns property in application.properties resource file. If the property does not exist, the table is created in the namespace ns2.
@NosqlTable(tableName = "#{ systemProperties['sys_ns'] != null ? systemProperties['sys_ns'] : @environment.getProperty('ENV_NS') != null ? @environment.getProperty('ENV_NS') : '${app.ns:srcNs}' }:Customer")
In this example, the namespace is evaluated in the following order:
  1. In the namespace defined by the JVM system property sys_ns.
  2. If sys_ns is not available, then environment variable ENV_NS is tried.
  3. If ENV_NS is not available, then the namespace defined by the app.ns property in application.properties resource file is tried.
  4. If none of the above are available, the Customer table is created in the srcNs namespace.
@NosqlTable(tableName = ":Customer")

The starting colon ':' is automatically ignored when SpEl expressions '#' and '$' are used and result is an "" empty string namespace.

In this example, an error is returned since neither of them are present.

For more details on namespace management, see Introducing Namespaces in the Java Direct Driver Developer's Guide.

Example:

Create the Student entity class and provide the required table name (Customer) and the namespace (JVM system property sys_ns) in the @NosqlTable annotation. The spring driver evaluates the SpEL expressions and the Customer table is created in sys_ns namespace. If the namespace does not exist, the table is created in the sysdefault namespace.
import com.oracle.nosql.spring.data.core.mapping.NosqlId;
import com.oracle.nosql.spring.data.core.mapping.NosqlTable;

/* The @NosqlTable annotation specifies that this class will be mapped to an Oracle NoSQL Database table. */

/* Sets the table name. */
@NosqlTable(tableName = "#{ systemProperties['sys_ns']}:Customer")

public class Student {
    /* The @NosqlId annotation specifies that this field will act as the ID field.
       The generated=true attribute specifies that this ID will be auto-generated by a sequence. */
    @NosqlId(generated = true)
    long id;
    String firstName;
    String lastName;
   

        /* public or package protected constructor required when retrieving from database. */
    public Student() {

        }
    /* This method overrides the toString() method, and then concatenates id, firstname, lastname, 
       and then returns a String. */
    @Override
    public String toString() {
        return "Student{" +
            "id=" + id + ", " +
            "firstName=" + firstName + ", " +
            "lastName=" + lastName +
            '}';  
    }
}