realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Reverse lookup (Backlinks)

Open tspecht opened this issue 12 years ago • 41 comments

Hi guys,

I looked at the documentation and found support for linked queries. Is reverse lookup currently possible with the java version of Realm? I'm also using Realm on Cocoa which just added support for reverse lookup and wondered if there is already support for this feature in Java.

Thanks a lot!

tspecht avatar Nov 24 '14 11:11 tspecht

Unfortunately no. It is not implemented on the Java side yet, but it on our todo.

cmelchior avatar Nov 24 '14 11:11 cmelchior

Hi, Not yet, but it's on the task list obviously :-) We got a few other issues we intend to tackle first though. So if anyone out there can't wait and have some spare cycles, feel free to create a PR :-)

Cheers,

On Mon, Nov 24, 2014 at 12:07 PM, tspecht [email protected] wrote:

Hi guys,

I looked at the documentation and found support for linked queries. Is reverse lookup currently possible with the java version of Realm? I'm also using Realm on Cocoa which just added support for reverse lookup and wondered if there is already support for this feature in Java.

Thanks a lot!

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

bmunkholm avatar Nov 24 '14 12:11 bmunkholm

This issue has been raised a few times now, so I have begun thinking about implementations:

Cocoa uses the following method: https://github.com/realm/realm-cocoa/blob/master/Realm/RLMObjectBase.mm (linkingObjectsOfClass)

and is documented here: http://realm.io/docs/cocoa/0.89.2/#inverse-relationships

I would propose something like this in Java

public class Owner extends RealmObject {
  private RealmList<Dog> dogs;
}

public class Dog extends RealmObject {
  private String name;

  @Backlink // Simple case where relevant field is automatically detected.
  private RealmList<Owner> owners; // Returns list of all owners pointing to dog. 

  @Backlink("dogs") // Advanced case for selecting exact property if multiple exists.
  private Owner owner; // This would throw an error if more than 1 owner existed or possible just return the first?
}

The Cocoa implementation allow custom filtering of the backlinks returned, but is that a use case we should support? I lean towards no.

It would also be nice to not have to specify which property to lookup (as it is not typed). Ideally the fieldName in @Backlink is optional and automatically finds the right property if there is only one with that type. Otherwise it should be required.

cmelchior avatar Jan 13 '15 07:01 cmelchior

Can you book a time to discuss this with @alazier + me when you get close to implementing this? There are specific reasons to use the design we are using in Cocoa rather than what you are proposing — not sure all of those reasons apply to Java, but worth a chat.

timanglade avatar Mar 04 '15 16:03 timanglade

We considered an approach similar to this in objective-c. The biggest issue with this is that there is no query support for back-links in core - so with this approach you are opening up a can of worms, where users will think they can query backlink properties like any other property even though this will not work.

Once core has support for querying backlinks like any other property I think an approach along these lines makes a bit more sense.

alazier avatar Mar 09 '15 19:03 alazier

So you can only navigate them, not query them....that sounds odd, but yes, not being able to query on them will be a serious drawback. I wonder how hard adding querying support would be.

cmelchior avatar Mar 09 '15 19:03 cmelchior

I don't think it'd take a lot to add support for the same queries that can be done on LinkList, but LinkList queries are currently missing quite a bit.

tgoyne avatar Mar 09 '15 20:03 tgoyne

:+1:

http://stackoverflow.com/questions/30425398/realmresult-many-many-relationship-with-adapter

ahornerr avatar May 26 '15 18:05 ahornerr

:+1:

kristjanmik avatar Sep 22 '15 19:09 kristjanmik

After using linkingObjects in Swift, coming back to Java and not having this requires some workaround, especially since #1216 is still open and custom methods on realm objects aren't allowed yet. Any progress on this? I like the idea of a @Backlink annotation.

jpiche avatar Oct 13 '15 16:10 jpiche

EDIT: Note that you can implement backlinks yourself see cmelchior post earlier. But it would be nice if it would be provided from Realm in the first place.

dasheck0 avatar Nov 09 '15 11:11 dasheck0

:+1:

dawidhyzy avatar Nov 30 '15 08:11 dawidhyzy

Is backlink not possible yet? 😔

lourenci avatar Jan 07 '16 18:01 lourenci

Not yet. We are awaiting being able to query on them in the core engine before they are implemented in the Java layer.

cmelchior avatar Jan 07 '16 18:01 cmelchior

:+1: Im dealing with this issue.

I have Posts, Answer and Profile objects all of them related to a User object (think of facebook) So several Posts, Answers and Profile can have a common User (the User object is not aware and does not store this relationship). When i delete Post, Answers or Profiles, i cannot delete the User since it might be pointed by other Post, Answer or Profile.

However, if i never delete User objects, i will end up with "zombie" Users stored but not pointed by anything. I need to use something like backlinks to find and delete objets that are no longer related to anything.

BTW: If there is another logical solution or workarround let me know!

guillermomuntaner avatar Jan 25 '16 22:01 guillermomuntaner

+1

dltlr avatar Mar 02 '16 15:03 dltlr

+1

juanagu avatar Mar 16 '16 15:03 juanagu

The proposed API for reverse lookups (backlinks) is current something like the following:

public class Owner extends RealmObject {
  private String name;
  private int age;
  private RealmList<Cat> cats;
}

public class Person extends RealmObject {
  private Cat firstCat;
  private Cat secondCat
}

public class Cat extends RealmObject {
  private String name;

  @Backlink // Simple case where relevant field is automatically detected.
  private RealmResults<Owner> owners; // Returns list of all owners pointing to this Cat. 

  @Backlink("firstCat") // Advanced case for selecting exact property if multiple exists.
  private RealmResults<Person> persons; 

  @Backlink("firstCat") 
  private Person person; // Only allow [0,1] parent objects. More will throw IllegalStateException when accessing it. 
}

// Queries
realm.where(Cat.class).equalTo("owners.name", "Diane").findAll();
realm.where(Cat.class).isNotNull("person").findAll();
realm.where(Cat.class).greaterThan("owners.@size", 1).findAll(); // This type of Query is not possible in Java yet.

Subtasks:

  • [ ] Add @Backlink annotation to the annotation processor
  • [ ] The byte code transformation must change the field accessor (cat.owners) to getting a RealmResults using Table::get_backlink_view() making something like cat.owners.where().equalTo("name", "John").findAll() possible
  • [ ] Extend createAndValidate() to check if @Backlink does point to a proper class/field
  • [ ] Extend query predicates (equalTo() etc.) to be able to query backlinks

kneth avatar Apr 12 '16 14:04 kneth

A few additional notes:

  • Swift docs are here: https://realm.io/docs/swift/latest/#inverse-relationships
  • In the first version we will not be able to support a single parent reference. Mostly because we are not enforcing that when inserting objects.

cmelchior avatar May 26 '16 05:05 cmelchior

When is this coming to Java?

ghost avatar Jun 30 '16 07:06 ghost