Skip to main content

Object references

Dari supports querying relationships using the field/subfield path notation in where methods. The following models demonstrate the use case of finding all activities for a particular user. In these models, the Activity.activityUser field is of type User.

showLineNumbers
1
public class Activity extends Record {
2
3
@Indexed private Date activityDate;
4
@Indexed private User activityUser;
5
@Indexed private String groupLead;
6
7
/* Getters and setters */
8
}
9
10
public class User extends Record {
11
12
@Indexed private String name;
13
14
/* Getters and setters */
15
}

There are two ways to find activities by a specific user. You can first query for the user, and then query for activities by that user.

showLineNumbers
1
/* Get the user */
2
User user = Query.from(User.class).where("name = 'John Smith'").first();
3
4
/* Get the user's list of activities */
5
List<Activity> activities = Query.from(Activity.class).where("activityUser = ?", user).selectAll();

A second, more efficient way, is to use a single query that incorporates path notation.

showLineNumbers
1
List<Activity> activities = Query.from(Activity.class)
2
.where("activityUser/name = 'John Smith'")
3
.selectAll();