cqengine加入两个以上collections
cqengine join more than two collections
假设我有 3 个 collections:
User
User_Role
Role
我想通过给定角色名称了解用户,但我需要在 user.id 上加入 user_role 并在 role.id 上加入 user_role 才能建立用户。目前所有 samples 仅演示如何与两个 collections 进行连接,即
Query<Car> carsQuery = and(
in(Car.FEATURES, "sunroof", "convertible"),
existsIn(garages,
Car.NAME,
Garage.BRANDS_SERVICED,
equal(Garage.LOCATION, "Dublin")
)
);
如何创建查询以从给定的 User user
中获取 ResultSet<Role>
?
这是我目前所拥有的,但我得到 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:
注意 - 请记住: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);
}
以上例子适用于Java8.
上找到可用于 Java 6/7 的代码版本
假设我有 3 个 collections:
User
User_Role
Role
我想通过给定角色名称了解用户,但我需要在 user.id 上加入 user_role 并在 role.id 上加入 user_role 才能建立用户。目前所有 samples 仅演示如何与两个 collections 进行连接,即
Query<Car> carsQuery = and(
in(Car.FEATURES, "sunroof", "convertible"),
existsIn(garages,
Car.NAME,
Garage.BRANDS_SERVICED,
equal(Garage.LOCATION, "Dublin")
)
);
如何创建查询以从给定的 User user
中获取 ResultSet<Role>
?
这是我目前所拥有的,但我得到 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:
注意 - 请记住: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);
}
以上例子适用于Java8.
上找到可用于 Java 6/7 的代码版本