首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cqengine连接多个集合

cqengine连接多个集合
EN

Stack Overflow用户
提问于 2016-11-01 10:37:33
回答 1查看 795关注 0票数 1

假设我有三个收藏品:

代码语言:javascript
复制
User
User_Role
Role

我想通过给定的角色名来了解用户,但是我需要加入user_role on user.id和user_role on role.id来建立用户。目前,所有样本只演示如何使用两个集合进行连接,即

代码语言:javascript
复制
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>)

代码语言:javascript
复制
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())
);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-02 23:31:30

下面显示了3 IndexedCollections之间的连接:

注意-记住:import static com.googlecode.cqengine.query.QueryFactory.*;

代码语言:javascript
复制
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
}

..given以下域对象-用户、角色和UserRole:

代码语言:javascript
复制
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站点这里这里上找到与Java6/7一起使用的代码版本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40358388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档