【问题标题】:cqengine join more than two collectionscqengine 加入两个以上的集合
【发布时间】:2016-11-01 10:37:33
【问题描述】:

假设我有 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")
                )
);

如何创建查询以从给定的 User user 获取 ResultSet&lt;Role&gt;


这是我目前所拥有的,但我收到了no suitable method found for and(Query&lt;User&gt;,Query&lt;Role&gt;,Equal&lt;Role,String&gt;)

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())
);

【问题讨论】:

    标签: java collections cqengine


    【解决方案1】:

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

    注意 - 请记住: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")));
    
    
        users.retrieve(usersWhoAreManagers)
                .forEach(u -> System.out.println(u.userName));
        // ..prints: Jane, Jesse
    }
    

    ..给定以下域对象 - 用户、角色和用户角色:

    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 站点 herehere 上找到可用于 Java 6/7 的代码版本。

    【讨论】:

      猜你喜欢
      • 2020-05-12
      • 2018-06-03
      • 2013-03-11
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2016-10-20
      相关资源
      最近更新 更多