Realm-java: Don't force objects to extend RealmObject

Created on 1 Oct 2014  Â·  20Comments  Â·  Source: realm/realm-java

Is it possible to save Object.class descendants directly, without extending RealmObject?

T-Enhancement

Most helpful comment

This is why U abadonned to use Realm anymore. Creating entities’s copy as RealmObject is ridiculous and useless extra work. :( There are alternatives that does the same thing with annotations, so I hope Realm will such smart lib once.

All 20 comments

It is not possible since we need RealmObject to store some important informations to be able to retrieve your data from the DB. It works in the same way in the Cocoa binding.

What is your use-case? Why is extending RealmObject an annoyance in your case?

Is this possible without getting this error?
( Error:A RealmClass annotated object must be derived from RealmObject )

Word.java

public class Word extends RealmObject {
    private String word;

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }
}

GreekWord.java

public class GreekWord extends Word{

    @Override
    public String getWord() {
        return super.getWord();
    }
}

Currently, it is not supported to subclass your own Realm classes (your GreekWord class above). The reason is that Realm will override the getters and setters, and you should not add any logic to them.

@kneth @emanuelez
This is real a deal breaker. I have lots of models which already extends each other. I just won't be able to use Realm.

Surely understand the need. We defintely will look into supporting this
going forward.
Until then it's often possible to use composition instead of inheritance.

//Brian

On Thu, Oct 2, 2014 at 3:00 PM, Igor Filippov [email protected]
wrote:

@kneth https://github.com/kneth @emanuelez
https://github.com/emanuelez
This is real a deal breaker. I have lots of models which already extends
each other. I just won't be able to use Realm.

—
Reply to this email directly or view it on GitHub
https://github.com/realm/realm-java/issues/440#issuecomment-57625689.

Is there a composition example?

That's actually in the first example in the docs:

public class User extends RealmObject {

private String          name;
private int             age;
private String          email;
private User            boss;

...

Here boss is a User, but could be any other RealmObject as well.

See also http://en.wikipedia.org/wiki/Composition_over_inheritance

On Thu, Oct 2, 2014 at 4:25 PM, Basilis Charalampakis <
[email protected]> wrote:

Is there a composition example?

—
Reply to this email directly or view it on GitHub
https://github.com/realm/realm-java/issues/440#issuecomment-57637191.

For now I use interfaces to provide functions for objects that normally would be inherited. So there is no need for too much casting

I also have a case where composition may not work - https://github.com/realm/realm-java/issues/735 . Inheritance will surely be useful.

Just to follow up on this. After an internal design discussion we have decided that the requirement to extend RealmObject is here to stay for the foreseeable future, so I am closing this enhancement here now.

However we certainly still want to support subclassing / polymorphism which instead will be tracked here: https://github.com/realm/realm-java/issues/761

I know that this issue is closed, but I want leave this comment.

I really liked this library, but I want to implement my domain classes whithou any infrastructure code. An example is the Hibernate mapping, which I as a architecture can separate the domain and the database code.
Forcing objects to extend RealmObject makes me unable to use DDD, as an example. http://martinfowler.com/tags/domain%20driven%20design.html

Thanks

One possible solution for achieving the best of both worlds could perhaps be to to develop a custom Realm IntelliJ plugin that did static analysis. This would obviously only work inside IntelliJ, but Google are doing the same with their support annotations: http://tools.android.com/tech-docs/support-annotations and they even managed to get it to work with the Gradle plugin.

Something like this:

// 1. We remove the RealmObject class
// 2. annotate all methods that require an RealmObject like so
public <E> E createObject(@RealmClass Class<E> class) {
 // ...
}

// 3. When calling this method we do static analysis and let IntelliJ mark it as an error
realm.createObject(MyRealmObject.class); // This would work
realm.createObject(NonRealmObject.class); // This would be marked as an error

Hey, I know this issue has been closed, but what if the RealmObject class has a field for _ID like the android BaseColumns class.

@danijax We have internally discussed adding a global and unique id to all objects. Many issues could be solved by doing so but so far we still believe that users can add primary keys when they need something this a unique identifier.

I have three model classes. Two of them extends RealmObject while the remaining one does not.
the first class extends the RealmObject;

public class Party extends RealmObject implements Parcelable {
    @PrimaryKey
    public int id;
    public String name;
    public String name_en;
    public String name_ne;
    public String address;
    public String phoneNumber;
    public String taxRegistrationNumber;
    public String partyType;

the second class holds a field of type Party. But this does not extends RealmObject

public class CreatePurchaseOrder implements Parcelable {
    public int voucherNumber;
    public Date date;
    public Party party;
    String agent;

The third class holds a field for CreatePurchaseOrder and extends RealmObject

public class CreatePurchaseOrderRow extends RealmObject implements Parcelable {
    @PrimaryKey
    public int id;
    private int serialNumber;
    private String specification;
    private float quantity;
    private float rate;
    private String remarks;
    private boolean fulfilled;
    private CreatePurchaseOrder createPurchaseOrder;

With this approach it generates an error message
error

Do i need to extend every class with RealmObject?

Thanks in advance,

@Suresh-Reddevil all of your classes have to either extend RealmObject or implement RealmModel

Hi,

Any idea when would this be implemented? Just an idea?

@zaheerahmad what is your use-case

@Zhuinden
I have a response coming up from server, in which we have class as Task, and there are different types of tasks.

For now, I have implemented this way:

@Index
@PrimaryKey
@SerializedName("TaskID")
private String taskId;

@SerializedName("TaskType")
private int taskType;

@Index
@SerializedName("Name")
private String name;

@SerializedName("Description")
private String description;

@SerializedName("BeginDateTime")
private String beginDateTime;

@SerializedName("EndDateTime")
private String endDateTime;

@Index
@SerializedName("StoreNumber")
private String storeNumber;

@SerializedName("CreatedBy")
private String createdBy;

@SerializedName("CreationDateTime")
private String creationDateTime;

@Index
@SerializedName("TaskStatusId")
private int taskStatusId;

@SerializedName("Modified")
private String modifiedDateTime;

@SerializedName("SubTaskCount")
private int subTaskCount;

//TODO: it should be a separate class and inherit CPUCTask class.

//Not supported in Realm at the moment
//Reference: https://github.com/realm/realm-java/issues/2643 and https://github.com/realm/realm-java/issues/440
@SerializedName("CountSubTasks")
private RealmList countSubTasks;

This is why U abadonned to use Realm anymore. Creating entities’s copy as RealmObject is ridiculous and useless extra work. :( There are alternatives that does the same thing with annotations, so I hope Realm will such smart lib once.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jjorian picture jjorian  Â·  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  Â·  3Comments

aschrijver picture aschrijver  Â·  3Comments

harshvishu picture harshvishu  Â·  3Comments

David-Kuper picture David-Kuper  Â·  3Comments