Hooli Hooli - 1 year ago 101
Java Question

cqengine join more than two collections

Say I have 3 collections:


I want to know a user by given role name but I need to join user_role on user.id and user_role on role.id to establish the user. Currently all the samples only demonstrate how to do a join with two collections, i.e.

Query<Car> carsQuery = and(
in(Car.FEATURES, "sunroof", "convertible"),
equal(Garage.LOCATION, "Dublin")

How do I create a query to get
from a given
User user

This is what I have so far but I'm getting
no suitable method found for and(Query<User>,Query<Role>,Equal<Role,String>)

String ROLE_NAME = "tester";
Query<User> query = and(
(Attribute<User, String>) (Object) User.ID_INDEX,
(Attribute<Role, String>) (Object) Role.ID_INDEX,
equal(Role.NAME_INDEX, ROLE_NAME.toUpperCase())

Answer Source

The following shows a JOIN between 3 IndexedCollections:

Note - remember to: import static com.googlecode.cqengine.query.QueryFactory.*;

public static void main(String[] args) {
    IndexedCollection<User> users = new ConcurrentIndexedCollection<>();
    users.add(new User(1, "Joe"));
    users.add(new User(2, "Jane"));
    users.add(new User(3, "Jesse"));

    IndexedCollection<Role> roles = new ConcurrentIndexedCollection<>();
    roles.add(new Role(1, "CEO"));
    roles.add(new Role(2, "Manager"));
    roles.add(new Role(3, "Employee"));

    IndexedCollection<UserRole> userRoles = new ConcurrentIndexedCollection<>();
    userRoles.add(new UserRole(1, 3)); // Joe is an Employee
    userRoles.add(new UserRole(2, 2)); // Jane is a Manager
    userRoles.add(new UserRole(3, 2)); // Jesse is a Manager

    // Retrieve Users who are managers...
    Query<User> usersWhoAreManagers =
            existsIn(userRoles, User.USER_ID, UserRole.USER_ID,
                    existsIn(roles, UserRole.ROLE_ID, Role.ROLE_ID, equal(Role.ROLE_NAME, "Manager")));

            .forEach(u -> System.out.println(u.userName));
    // ..prints: Jane, Jesse

..given the following domain objects - User, Role, and UserRole:

public class User {
    final int userId;
    final String userName;

    public User(int userId, String userName) {
        this.userId = userId;
        this.userName = userName;

    static final Attribute<User, Integer> USER_ID   = attribute(u -> u.userId);
    static final Attribute<User, String>  USER_NAME = attribute(u -> u.userName);

public class Role {
    final int roleId;
    final String roleName;

    public Role(int roleId, String roleName) {
        this.roleId = roleId;
        this.roleName = roleName;

    static final Attribute<Role, Integer> ROLE_ID   = attribute(r -> r.roleId);
    static final Attribute<Role, String>  ROLE_NAME = attribute(r -> r.roleName);

public class UserRole {
    final int userId;
    final int roleId;

    public UserRole(int userId, int roleId) {
        this.userId = userId;
        this.roleId = roleId;

    static final Attribute<UserRole, Integer> USER_ID = attribute(ur -> ur.userId);
    static final Attribute<UserRole, Integer> ROLE_ID = attribute(ur -> ur.roleId);

The example above applies to Java 8.

You can also find a version of the code which can be used with Java 6/7 on the CQEngine site here and here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download