[英]How to add HashMap to List of Employee class
I have created a Java class and using JPA to retrieving details of Employee class which is returning list of employees like我创建了一个 Java class 并使用 JPA 检索员工 ZA2F2ED4F8EBC2CBB1DZC21 的详细信息
[
{
"empId": "1",
"empName": "emp1",
"empTag": "empTag1",
"empState": "ACTIVE"
},
{
"empId": "2",
"empName": "emp2",
"empTag": "empTag2",
"empState": "ACTIVE"
}
]
After receiving list of hash I want to add few dummy entries like在收到 hash 的列表后,我想添加一些虚拟条目,例如
[
{
"empId": "00",
"empName": "DummyEmp",
"empTag": "DummyEmpTag",
"empState": "ACTIVE"
},
{
"empId": "1",
"empName": "emp1",
"empTag": "empTag1",
"empState": "ACTIVE"
},
{
"empId": "2",
"empName": "emp2",
"empTag": "empTag2",
"empState": "ACTIVE"
}
]
So I am trying to add map into list of employee but at the run time I am getting所以我试图将 map 添加到员工列表中,但在运行时我得到了
java.lang.ClassCastException: java.util.HashMap$KeySet cannot be cast to com.domain.EmployeeBean
My Employee Class:我的员工 Class:
@Table(name = "employee")
@Entity
public class EmployeeBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id @Column(name = "emp_id")
private String empId;
@Column(name = "emp_name")
private String empName;
@Column(name = "emp_tag")
private String empTag;
@Column(name = "emp_state")
private String empState;
.... (getters and setters)
}
Repository:-存储库:-
public interface EmployeeBeanRepository extends JpaRepository<EmployeeBean, String>{
List < EmployeeBean > findByEmpState(String empState);
Resource:-资源:-
@RestController
@RequestMapping("/empDetails")
public class EmployeeBeanResource {
private final EmployeeBeanRepository empBeanRepository;
public EmployeeBeanResource(EmployeeBeanRepository empBeanRepository) {
this.empBeanRepository = empBeanRepository;
}
@GetMapping("/empState/{empState}")
@Timed
public List<EmployeeBean> getEmployeesByStatus(@PathVariable String empState) {
List<EmployeeBean> activeEmployeeList = empBeanRepository.findByEmpState(empState);
Map<String, String> empDummy = new HashMap<>();
empDummy.put("empId", "00");
empDummy.put("empName", "DummyEmp");
empDummy.put("empTag", "DummyEmpTag");
empDummy.put("empState", "ACTIVE");
activeEmployeeList.add((EmployeeBean) empDummy.keySet());
activeEmployeeList.add((EmployeeBean) empDummy.values());
return activeEmployeeList;
}
}
I have tried a lot but still unable to figure out a way to add map in to list of employee.我已经尝试了很多,但仍然无法找到将 map 添加到员工列表中的方法。
Instead of this:而不是这个:
Map<String, String> empDummy = new HashMap<>();
empDummy.put("empId", "00");
empDummy.put("empName", "DummyEmp");
empDummy.put("empTag", "DummyEmpTag");
empDummy.put("empState", "ACTIVE");
activeEmployeeList.add((EmployeeBean) empDummy.keySet());
try the following:尝试以下操作:
EmployeeBean empDummy = new EmployeeBean();
empDummy.setEmpId("00");
empDummy.setEmpName("DummyEmp");
empDummy.setEmpTag("DummyEmpTag");
empDummy.setEmpState("ACTIVE");
activeEmployeeList.add(empDummy);
or if you have all args constructor:或者如果你有所有 args 构造函数:
EmployeeBean empDummy = new EmployeeBean("00", "DummyEmp","DummyEmpTag","ACTIVE");
activeEmployeeList.add(empDummy);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.