说我有3个收藏:
User
User_Role
Role
我想通过给定的角色名来了解用户,但我需要在role.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())
);
答案 0 :(得分:2)
以下显示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
}
..给出以下域对象 - 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.