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
/ /
Java SDK

Model Data - Java SDK

Nota

Las nuevas aplicaciones SDK de Java no pueden usar RealmAny

Nuevo Servicios de aplicaciones Las aplicaciones no podrán sincronizar modelos de datos con propiedades de tipo RealmAny.

An object schema is a configuration object that defines the fields and relationships of a Realm object type. Android Realm applications define object schemas with Java or Kotlin classes using Realm Schemas.

Object schemas specify constraints on object fields such as the data type of each field, whether a field is required, and default field values. Schemas can also define relationships between object types in a realm.

Modifying your application's Realm Schema requires you to migrate data from older versions of your Realm Schema to the new version.

Cada aplicación tiene un Esquema Realm compuesto por una lista de esquemas de objetos para cada tipo de objeto que pueden contener los realms en esa aplicación.

Realm garantiza que todos los objetos de un reino se ajusten al esquema para su tipo de objeto y valida los objetos cada vez que se crean, modifican o eliminan.

Apps that use Atlas Device Sync can define schemas in two ways:

Puedes modelar relaciones uno-a-uno en realm con campos RealmObject. Puedes modelar relaciones uno-a-muchos y muchos-a-uno con campos RealmList. Las relaciones inversas son el extremo opuesto de una relación uno-a-muchos o muchos-a-uno. Puedes hacer que las relaciones inversas sean transitables con la anotación @LinkingObjects en un campo RealmResults. En una instancia de un RealmObject, los campos de relación inversa contienen el conjunto de objetos Realm que apuntan a esa instancia de objeto a través de la relación descrita. Puedes encontrar el mismo conjunto de objetos Realm con una query manual, pero el campo de relación inversa reduce el código redundante de query y la capacidad de errores.

Unlike normal Java objects, which contain their own data, a Realm object doesn't contain data. Instead, Realm objects read and write properties directly to Realm.

Instances of Realm objects can be either managed or unmanaged.

  • Managed objects are:

    • persistente en Realm

    • siempre actualizado

    • thread-confined

    • Generalmente son más livianos que la versión no administrada, ya que ocupan menos espacio en el montón de Java.

  • Unmanaged objects are just like ordinary Java objects, since they are not persisted and never update automatically. You can move unmanaged objects freely across threads.

You can convert between the two states using realm.copyToRealm() and realm.copyFromRealm().

Las clases RealmProxy son la forma en que el SDK de Realm garantiza que los objetos de Realm no contengan datos. En su lugar, el RealmProxy de cada clase accede directamente a los datos en la base de datos.

For every model class in your project, the Realm annotation processor generates a corresponding RealmProxy class. This class extends your model class and is returned when you call Realm.createObject(). In your code, this object works just like your model class.

Realm objects:

  • cannot contain fields that use the final or volatile modifiers (except for inverse relationship fields).

  • cannot extend any object other than RealmObject.

  • must contain an empty constructor (if your class does not include any constructor, the automatically generated empty constructor will suffice)

Naming limitations:

  • Los nombres de clase no pueden exceder los 57 caracteres.

  • Los nombres de clase deben ser únicos dentro de los módulos del reino

  • Field names cannot exceed 63 characters.

Size limitations:

  • String o los campos byte[] no pueden exceder los 16 MB.

Usage limitations:

  • Because Realm objects are live and can change at any time, their hashCode() value can change over time. As a result, you should not use RealmObject instances as a key in any map or set.

The bytecode transformer used by Realm supports incremental builds, but your application requires a full rebuild when adding or removing the following from a Realm object field:

  • an @Ignore annotation

  • la palabra clave static

  • la palabra clave transient

Puede realizar una reconstrucción completa con Build > Clean Project y Build > Rebuild Project en estos casos.

A schema version identifies the state of a Realm Schema at some point in time. Realm tracks the schema version of each realm and uses it to map the objects in each realm to the correct schema.

Schema versions are integers that you may include in the realm configuration when you open a realm. If a client application does not specify a version number when it opens a realm then the realm defaults to version 0.

Importante

Increment Versions Monotonically

Las migraciones deben actualizar un realm a una versión de esquema superior. Realm arroja un error si una aplicación cliente abre un realm con una versión de esquema inferior a la versión actual del realm o si la versión de esquema especificada es la misma que la versión actual del realm pero incluye diferentes esquemas de objetos.

A local migration is a migration for a realm that does not automatically Sync with another realm. Local migrations have access to the existing Realm Schema, version, and objects and define logic that incrementally updates the realm to its new schema version. To perform a local migration you must specify a new schema version that is higher than the current version and provide a migration function when you open the out-of-date realm.

With the SDK, you can update underlying data to reflect schema changes using manual migrations. During such a manual migration, you can define new and deleted properties when they are added or removed from your schema. The editable schema exposed via a DynamicRealm provides convenience functions for renaming fields. This gives you full control over the behavior of your data during complex schema migrations.

Tip

Migrations During Application Development

During development of an application, RealmObject classes can change frequently. You can use Realm.deleteRealm() to delete the database file and eliminate the need to write a full migration for testing data.

Volver

Encrypt a Realm

En esta página