Previous Next JavaScript must be enabled to correctly display this content

## Using SpEl expressions in `NosqlTable` annotation

Learn about using Spring Expression Language (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 `Student` class example discussed in previous topics, 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.

SpEl is a way to evaluate complex expressions at runtime. 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 runtime.

Table 2-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: If the namespace defined by the JVM system property `sys_ns`. If `sys_ns` is not available, then environment variable `ENV_NS` is tried. If `ENV_NS` is not available, then the namespace defined by the `app.ns` property in `application.properties` resource file is tried. If none of the previously mentioned namespaces 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 2-3 Using SpEl expressions in the table name

The following example shows how to create the `Student` entity class and provide the table name as `Customer` in the namespace (JVM system property `sys_ns`) using the `@NosqlTable` annotation.

The Spring Data 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 autogenerated 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 +
                '}';
        }
    }