Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
io.realm.annotations

Enum RealmNamingPolicy

java.lang.Object
io.realm.annotations.RealmNamingPolicy

Esta enumeración define las posibles formas en que los nombres de clases y campos se pueden asignar desde lo que se usa en Java al nombre usado internamente en el archivo Realm. Ejemplos donde esto es útil:

  • To support two model classes with the same simple name but in different packages.

  • Para facilitar el trabajo con esquemas multiplataforma, ya que las convenciones de nomenclatura son distintas.

  • To use a Java class name that is longer than the 57 character limit enforced by Realm.

  • To change a field name in Java without forcing app users through a migration process.

Depending on where the policy is applied, it will have slightly different semantics:

  • Si se aplica a RealmModule.classNamingPolicy todas las clases que formen parte de ese módulo se verán afectadas. Si una clase forma parte de varios módulos, se debe aplicar la misma política de nomenclatura a ambos módulos; de lo contrario, se generará un error.

  • Si se aplica a RealmModule.fieldNamingPolicy todos los campos persistibles en todas las clases que forman parte de este módulo se verán afectados.

  • Si se aplica a RealmClass.fieldNamingPolicy, todos los campos de esa clase se verán afectados. Esto anulará cualquier política de nombres de campos especificada en un módulo.

An example of this:

@RealmClass(name = "__person", fieldNamingPolicy = RealmNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
public class Person implements RealmModel { // is converted to "__person" internally
public string firstName; // Is converted to "first_name" internally
}

Choosing an internal name that differs from the name used in the Java model classes has the following implications:

  • Consultas sobre DynamicRealm Debe usar el nombre interno. Las consultas en instancias normales de Realm deben seguir usando el nombre tal como está definido en la clase Java.

  • Migrations must use the internal name when creating classes and fields.

  • Los errores de esquema notificados utilizarán los nombres internos.

When automatically converting Java variable names, each variable name is normalized by splitting it into a list of words that are then joined using the rules of the target format. The following heuristics are used for determining what constitutes a "word".

  1. Anytime a _ or $ is encountered. Examples are "_FirstName", "_First_Name" and "$First$Name" which all becomes "First" and "Name".

  2. Cada vez que cambie de un carácter en minúscula a uno en mayúscula según lo identificado por Character.isUpperCase(int) y Character.isLowerCase(int) . El ejemplo es "FirstName", que se convierte en "First" y "Name".

  3. Anytime you switch from more than one uppercase character to a lower case one. The last upper case letter is assumed to be part of the next word. This is identified by using Character.isUpperCase(int) and Character.isLowerCase(int) . Example is "FIRSTName" which becomes "FIRST" and "Name.

  4. Some characters like emojiis are neither uppercase nor lowercase characters, so they will be part of the current word. Examples are "my😁" and "MY😁" which are both treated as one word.

  5. Hungarian notation, i.e. variable names starting with lowercase "m" followed by uppercase letter is stripped and not considered part of any word. Example is "mFirstName" and "mFIRSTName" which becomes "First" and "Name.

Note that changing the internal name does NOT affect importing data from JSON. The JSON data must still follow the names as defined in the Realm Java class.

Cuando se trata de analizar JSON con librerías estándar como Moshi, GSON o Jackson, es importante recordar que estas librerías definen la transformación de JSON a Java, mientras que establecer nombres internos de Realm define la transformación de Java al archivo Realm.

This means that if you want to import data into Realm from JSON using these libraries you still need to provide the annotations from both the JSON parser library and Realm.

Using Moshi, it would look something like this:

public class Person extends RealmObject {
@Json(name = "first_name") // Name used in JSON input.
@RealmField(name = "first_name") // Name used internally in the Realm file.
public string firstName; // name used in Java
}

Tip

Enum Constant and Description

CAMEL_CASE

The name in the Java model class is converted to camelCase, i.e.

IDENTITY

El nombre en la clase del modelo Java se usa tal cual internamente.

MINÚSCULAS_CON_GUIONES_BAJOS

El nombre en la clase del modelo Java se convierte a minúsculas y cada palabra está separada por _ .

NO_POLICY

No policy is applied.

PASCAL_CASE

El nombre en la clase de modelo de Java se convierte a PascalCase, es decir.

Modificador y Tipo
Método y descripción

public static RealmNamingPolicy

public static RealmNamingPolicy

  • Methods inherited from class java.lang.Object : getClass , hashCode , equals , clone , toString , notify , notifyAll , wait , wait , wait , finalize

  • Métodos heredados de la clase java.lang.Enum : name , ordinal , toString , equals , hashCode , clone , compareTo , getDeclaringClass , valueOf , finalize

public static final RealmNamingPolicy

The name in the Java model class is converted to camelCase, i.e. all words are joined together with the first letter in the first word lower cased, and the first letter of all subsequent words upper cased. This is the standard naming schema in Java, Kotlin, Swift and JavaScript.Examples: "firstName", "FirstName", "mFirstName", "FIRST_NAME", "First$Name" all becomes "firstName".

public static final RealmNamingPolicy

El nombre en la clase del modelo Java se usa tal cual internamente.

public static final RealmNamingPolicy

El nombre de la clase del modelo Java se convierte a minúsculas, con cada palabra separada por _ . Este es el esquema de nombres predeterminado en C++.

Examples: "firstName", "FirstName", "mFirstName", "FIRST_NAME", "First$Name" all becomes "first_name".

public static final RealmNamingPolicy

No se aplica ninguna política. Esta política no sustituirá ninguna política establecida en un elemento padre, por ejemplo, si se establece en RealmClass.fieldNamingPolicy , la política del módulo seguirá aplicándose a los nombres de campo.

If two modules disagree on the policy and one of them is NO_POLICY , the other will be chosen without an error being thrown.

This policy is the default.

public static final RealmNamingPolicy

The name in the Java model class is converted to PascalCase, i.e. all words are joined together with the first letter of all words upper cased. This is the default naming scheme in .NET.Examples: "firstName", "FirstName", "mFirstName", "FIRST_NAME", "First$Name" all becomes "FirstName".

public static RealmNamingPolicy valueOf (
String name
)
public static RealmNamingPolicy values ()

Volver

RealmModule

En esta página