A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/java/model-data/define-a-realm-object-model/ below:

Define a Realm Object Model - Java SDK - Atlas Device SDKs

To define a Realm object in your application, create a subclass of RealmObject or implement RealmModel.

Important Note

Class names are limited to a maximum of 57 UTF-8 characters.

The following code block shows a Realm object that describes a Frog. This Frog class can be stored in Realm because it extends the RealmObject class.

import io.realm.RealmObject;public class Frog extends RealmObject {    private String name;    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectopen class Frog(    var name: String? = null,    var age: Int = 0,    var species: String? = null,    var owner: String? = null) : RealmObject() 

The following code block shows a Realm object that describes a Frog. This Frog class can be stored in Realm because it implements the RealmModel class and uses the @RealmClass annotation:

import io.realm.RealmModel;import io.realm.annotations.RealmClass;@RealmClasspublic class Frog implements RealmModel {    private String name;    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog() {}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
Important

All Realm objects must use the public visibility modifier.

import io.realm.RealmModelimport io.realm.annotations.RealmClass@RealmClassopen class Frog : RealmModel {    var name: String? = null    var age = 0    var species: String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }
Important

All Realm objects must use the open visibility modifier.

Tip Using RealmObject Methods

When you create a Realm object by extending the RealmObject class, you can access RealmObject class methods dynamically on instances of your Realm object. Realm objects created by implementing RealmModel can access those same methods statically through the RealmObject class:

frogRealmObject.isValid();frogRealmObject.addChangeListener(listener);RealmObject.isValid(frogRealmModel);RealmObject.addChangeListener(frogRealmModel, listener);
frogRealmObject?.isValidfrogRealmObject?.addChangeListener(listener)RealmObject.isValid(frogRealmModel)RealmObject.addChangeListener(frogRealmModel, listener)

Realm objects can contain lists of non-Realm-object data types:

Unlike lists of Realm objects, these lists can contain null values. If null values shouldn't be allowed, use the @Required annotation.

import io.realm.RealmList;import io.realm.RealmObject;public class Frog extends RealmObject {    private String name;    private int age;    private String species;    private String owner;    private RealmList<String> favoriteColors;    public Frog(String name, int age, String species, String owner, RealmList<String> favoriteColors) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;        this.favoriteColors = favoriteColors;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }    public RealmList<String> getFavoriteColors() { return favoriteColors; }    public void setFavoriteColors(RealmList<String> favoriteColors) { this.favoriteColors = favoriteColors; }}
import io.realm.RealmListimport io.realm.RealmObjectopen class Frog : RealmObject {    var name: String? = null    var age = 0    var species: String? = null    var owner: String? = null    var favoriteColors : RealmList<String>? = null    constructor(        name: String?,        age: Int,        species: String?,        owner: String?,        favoriteColors: RealmList<String>?    ) {        this.name = name        this.age = age        this.species = species        this.owner = owner        this.favoriteColors = favoriteColors    }    constructor() {} }

Realm provides the ability to nest objects within other objects. This has several advantages:

To embed an object, set the embedded property of the @RealmClass annotation to true on the class that you'd like to nest within another class:

import io.realm.RealmObject;import io.realm.annotations.RealmClass;@RealmClass(embedded=true)public class Fly extends RealmObject {    private String name;    public Fly(String name) {        this.name = name;    }    public Fly() {} }
import io.realm.RealmObjectimport io.realm.annotations.RealmClass@RealmClass(embedded = true)open class Fly : RealmObject {    private var name: String? = null    constructor(name: String?) {        this.name = name    }    constructor() {} }

Then, any time you reference that class from another class, Realm will embed the referenced class within the enclosing class, as in the following example:

import io.realm.RealmObject;public class Frog extends RealmObject {    private String name;    private int age;    private String species;    private String owner;    private Fly lastMeal;    public Frog(String name, int age, String species, String owner, Fly lastMeal) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;        this.lastMeal = lastMeal;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }    public Fly getLastMeal() { return lastMeal; }    public void setLastMeal(Fly lastMeal) { this.lastMeal = lastMeal; }}
import io.realm.RealmObjectopen class Frog : RealmObject {    var name: String? = null    var age = 0    var species: String? = null    var owner: String? = null    var lastMeal: Fly? = null    constructor(        name: String?,        age: Int,        species: String?,        owner: String?,        lastMeal: Fly?    ) {        this.name = name        this.age = age        this.species = species        this.owner = owner        this.lastMeal = lastMeal    }    constructor() {} }

Use annotations to customize your Realm object models.

New in version 10.6.0: Realm automatically indexes primary key fields. Previously, Realm only indexed String primary keys automatically.

Realm treats fields marked with the @PrimaryKey annotation as primary keys for their corresponding object schema. Primary keys are subject to the following limitations:

You can create a primary key with any of the following types:

Non-primitive types can contain a value of null as a primary key value, but only for one object of a particular type, since each primary key value must be unique. Attempting to insert an object with an existing primary key into a realm will result in a RealmPrimaryKeyConstraintException.

Realm automatically indexes primary key fields, which allows you to efficiently read and modify objects based on their primary key.

You cannot change the primary key field for an object type after adding any object of that type to a realm. If you are using Sync, you cannot change the primary key field for an object after defining the primary key in your backend schema.

Embedded objects cannot contain primary keys.

You may optionally define a primary key for an object type as part of the object schema with the @PrimaryKey annotation:

import io.realm.RealmObject;import io.realm.annotations.PrimaryKey;public class Frog extends RealmObject {    @PrimaryKey private String name;    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.PrimaryKeyopen class Frog : RealmObject {    @PrimaryKey var name : String? = null    var age = 0    var species: String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }
import io.realm.RealmObject;import io.realm.annotations.Required;public class Frog extends RealmObject {    @Required private String name;    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.Requiredopen class Frog : RealmObject {    @Required var name: String? = null    var age = 0    var species: String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }

Fields marked with Java object types and Kotlin nullable types (ending with ?) are nullable by default. All other types (primitives, non-nullable Kotlin object types) are required by default. You can mark a nullable field with the @Required annotation to prevent that field from holding a null value. RealmLists are never nullable, but you can use the @Required annotation to prevent objects in a list from holding a null value, even if the base type would otherwise allow it. You cannot mark a RealmList of RealmObject subtypes as required.

You can make any of the following types required:

Primitive types such as int and the RealmList type are implicitly required. Fields with the RealmObject type are always nullable, and cannot be made required.

Important Kotlin Types and Nullability

In Kotlin, types are non-nullable by default unless you explicitly add a ? suffix to the type. You can only annotate nullable types. Using the @Required annotation on non-nullable types will fail compilation.

Nullable fields are optional by default in Realm, unless otherwise specified with the @Required annotation. The following types are nullable:

Primitive types like int and long are non-nullable by default and cannot be made nullable, as they cannot be set to a null value.

In Kotlin, fields are considered nullable only if a field is marked nullable with the Kotlin ? operator except for the following types:

You can require any type that ends with the Kotlin ? operator, such as Int?.

The RealmList type is non-nullable by default and cannot be made nullable.

To assign a default value to a field, use the built-in language features to assign default values.

Use the class constructor(s) to assign default values:

import io.realm.RealmObject;public class Frog extends RealmObject {    private String name = "Kitty";    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}

Assign default values in the field declaration:

import io.realm.RealmObjectopen class Frog : RealmObject {    var name = "Kitty"    var age = 0    var species: String? = null    var owner: String? = null    constructor(name: String, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }
Note Default Values and Nullability

While default values ensure that a newly created object cannot contain a value of null (unless you specify a default value of null), they do not impact the nullability of a field. To make a field non-nullable, see Required Fields.

Indexes support the efficient execution of queries in Realm. Without indexes, Realm must perform a collection scan, i.e. scan every document in a collection, to select those documents that match a query. If an appropriate index exists for a query, Realm can use the index to limit the number of documents that it must inspect.

Indexes are special data structures that store a small portion of a realm's data in an easy to traverse form. The index stores the value of a specific field ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations.

Adding an index can speed up some queries at the cost of slightly slower write times and additional storage and memory overhead. Indexes require space in your realm file, so adding an index to a property will increase disk space consumed by your realm file. Each index entry is a minimum of 12 bytes.

You can index fields with the following types:

Realm creates indexes for fields annotated with @Index.

To index a field, use the @Index annotation:

import io.realm.RealmObject;import io.realm.annotations.Index;public class Frog extends RealmObject {    private String name;    private int age;    @Index private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.Indexopen class Frog : RealmObject {    var name: String? = null    var age = 0    @Index var species : String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }

If you don't want to save a field in your model to a realm, you can ignore a field.

Ignore a field from a Realm object model with the @Ignore annotation:

import io.realm.RealmObject;import io.realm.annotations.Ignore;public class Frog extends RealmObject {    private String name;    private int age;    private String species;        @Ignore private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.Ignoreopen class Frog : RealmObject {    var name: String? = null    var age = 0    var species: String? = null        @Ignore var owner  : String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }
Note The SDK ignores static and transient Fields

Fields marked static or transient are always ignored, and do not need the @Ignore annotation.

By default, Realm uses the name defined in the model class to represent fields internally. In some cases you might want to change this behavior:

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

Use the @RealmField annotation to rename a field:

import io.realm.RealmObject;import io.realm.annotations.RealmField;public class Frog extends RealmObject {    private String name;    private int age;    @RealmField("latinName") private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.RealmFieldopen class Frog : RealmObject {    var name: String? = null    var age = 0    @RealmField("latinName") var species: String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }

Alternatively, you can also assign a naming policy at the module or class levels to change the way that Realm interprets field names.

You can define a naming policy at the module level, which will affect all classes included in the module:

import io.realm.annotations.RealmModule;import io.realm.annotations.RealmNamingPolicy;@RealmModule( allClasses = true, classNamingPolicy = RealmNamingPolicy.LOWER_CASE_WITH_UNDERSCORES, fieldNamingPolicy = RealmNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)public class MyModule {}
import io.realm.annotations.RealmModuleimport io.realm.annotations.RealmNamingPolicy@RealmModule( allClasses = true, classNamingPolicy = RealmNamingPolicy.LOWER_CASE_WITH_UNDERSCORES, fieldNamingPolicy = RealmNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)open class MyModule

You can also define a naming policy at the class level, which overrides module level settings:

import io.realm.RealmObject;import io.realm.annotations.RealmClass;import io.realm.annotations.RealmNamingPolicy;@RealmClass(fieldNamingPolicy = RealmNamingPolicy.PASCAL_CASE)public class Frog extends RealmObject {    private String name;    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.RealmClassimport io.realm.annotations.RealmNamingPolicy@RealmClass(fieldNamingPolicy = RealmNamingPolicy.PASCAL_CASE)open class Frog : RealmObject {    var name: String? = null    var age = 0    var species: String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }

By default, Realm uses the name defined in the model class to represent classes internally. In some cases you might want to change this behavior:

Use the @RealmClass annotation to rename a class:

import io.realm.RealmObject;import io.realm.annotations.RealmClass;@RealmClass(name = "ShortBodiedTaillessAmphibian")public class Frog extends RealmObject {    private String name;    private int age;    private String species;    private String owner;    public Frog(String name, int age, String species, String owner) {        this.name = name;        this.age = age;        this.species = species;        this.owner = owner;    }    public Frog(){}     public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }    public String getSpecies() { return species; }    public void setSpecies(String species) { this.species = species; }    public String getOwner() { return owner; }    public void setOwner(String owner) { this.owner = owner; }}
import io.realm.RealmObjectimport io.realm.annotations.RealmClass@RealmClass(name = "Short_Bodied_Tailless_Amphibian")open class Frog : RealmObject {    var name: String? = null    var age = 0    var species: String? = null    var owner: String? = null    constructor(name: String?, age: Int, species: String?, owner: String?) {        this.name = name        this.age = age        this.species = species        this.owner = owner    }    constructor() {} }

By default, your application's Realm Schema includes all classes that extend RealmObject. If you only want to include a subset of classes that extend RealmObject in your Realm Schema, you can include that subset of classes in a module and open your realm using that module:

import io.realm.annotations.RealmModule;@RealmModule(classes = { Frog.class, Fly.class })public class MyModule {}
import io.realm.annotations.RealmModule@RealmModule(classes = [Frog::class, Fly::class])open class MyModule

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4