java - cqengine 连接两个以上集合

标签 java collections cqengine

假设我有 3 个集合:

User
User_Role
Role

我想通过给定的角色名称了解用户,但我需要在 user.id 上加入 user_role 并在 role.id 上加入 user_role 来建立用户。目前全部samples仅演示如何对两个集合进行连接,即

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

如何创建查询来获取 ResultSet<Role>从给定的User user

<小时/>

这是我到目前为止所拥有的,但我得到 no suitable method found for and(Query<User>,Query<Role>,Equal<Role,String>)

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

最佳答案

以下显示了 3 个 IndexedCollections 之间的 JOIN:

注意 - 请记住:导入 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")));


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

..给定以下域对象 - User、Role 和 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);
}

上面的示例适用于 Java 8。

您还可以在 CQEngine 站点 here 上找到可与 Java 6/7 一起使用的代码版本。和 here .

关于java - cqengine 连接两个以上集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358388/

相关文章:

java - JPA remove() 一个 deteched 实体 : find() vs merge() - Which is better?

java - 如何在map.putAll()中获取对象的旧值?

java - Oracle 不同与 java (cqengine/set) : whose leads to better performances?

java - 如何在Struts中包含多个消息资源?

java - 将 WildFly Application Server 的插件添加到 NetBeans

java - 如何在 Javafx 中启用/禁用选项卡?

c# - 为什么 Stack、Queue 和 List 在删除项目后不收缩内部数组?

java - HiLo 生成器策略不起作用

java - 对于快速变化的集合,CQEngine 中的索引惩罚是什么?

java - cqengine 按日期时间比较