gpt4 book ai didi

java - cqengine 连接两个以上集合

转载 作者:行者123 更新时间:2023-11-30 02:45:20 24 4
gpt4 key购买 nike

假设我有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com