如何解决如何在 SpringBoot 中使用另一个实体搜索一个实体在地图中?
我有一个实体“用户”和一个实体“工人”。现在实体工作者有一个地图“timeMap”,其中存储了
“用户”应在特定时间使用身份验证代码“预订”工作人员。然后“工人”可以看到哪个“用户”在什么时间注册了,“用户”可以看到他在什么时间注册了哪个“工人”。
用户类:
@Entity
public class User implements Serializable {
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String firstname;
private String surename;
private String email;
@Column(unique = true,name = "authCode")
private String authCode;
@SuppressWarnings("unused")
public User() {
}
public User (String firstname,String surename,String email,String authCode) {
this.firstname= firstname;
this.surename= surename;
this.email = email;
this.authCode = authCode;
}
...
}
工人类:
@Entity
public class Worker implements Serializable {
@Id
@GeneratedValue
private long id;
@Column(unique = true,name = "worker_id")
private String username;
private String firstname;
private String surename;
private String email;
private String password;
@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "users_roles",joinColumns = @JoinColumn(name = "worker_id"),inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
@OneToMany(cascade = CascadeType.ALL)
private Map<LocalTime,User> timeList;
@SuppressWarnings("unused")
public Worker() {
}
public Worker(String firstname,String password,Set<Role> roles) {
this.firstname = firstname;
this.surename = surename;
this.email = email;
this.password = password;
this.roles = roles;
this.username = createUsername();
}
...
在存储库中(或稍后在我的服务类中)编写方法以搜索具有“用户”的所有工作人员并返回包含所有预订时间和工作人员的 Map
例如 用户输入他的授权码。这将在 UserRepository 中搜索用户实体。 现在应该使用它在 WorkerRepository 中搜索用户及其时间的所有 Worker Entites 的所有地图条目。
例如
Worker1->Map (5:00,User1; 5:10,User2; 5:20,User3)
Worker2-> 的地图(5:20,User1; ....)
用户 1 的结果: 用户 1 --> (5:00,Worker1; 5:20,Worker2)
解决方法
这应该可以工作(如果您的项目在 Java/JDK 1.8+ 版上运行)。虽然我无法测试它。我希望这对你有用:
User userToUseAsFilter = /*<The user reference you want to search for>*/;
Map<LocalTime,Worker> map =
workerList.stream()
.flatMap(w -> w.getTimeList().entrySet().stream()
.map(ue -> new EntryHolder(w,ue.getKey(),ue.getValue())))
.filter(eh -> Objects.equals(eh.user,userToUseAsFilter))
.collect(Collectors.toMap(
eh -> eh.time,eh -> eh.worker,(w1,w2) -> w1 // Cannot combine two workers into one instance,therefore just ignore worker 2 if they both have the same local time
));
您还需要以下类来临时保存一些值:
public class EntryHolder {
public final Worker worker;
public final LocalTime time;
public final User user;
public EntryHolder(Worker worker,LocalTime time,User user) {
this.worker = worker;
this.time = time;
this.user = user;
}
}
您还应该在用户类中添加一个 equals 方法,以便在地图中真正找到用户(我使用 IntelliJ 中的 generate 功能自动生成这两个方法):
public class User implements Serializable {
... // All of your fields are here
// Auto generated using IntelliJ:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id && Objects.equals(firstname,user.firstname) && Objects.equals(surename,user.surename) && Objects
.equals(email,user.email) && Objects.equals(authCode,user.authCode);
}
// Auto generated using IntelliJ:
@Override
public int hashCode() {
return Objects.hash(id,firstname,surename,email,authCode);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。