实体类莫名其妙出现类型转化异常,主要是子类类型转换异常,自身转自身、遍历也报错
程序员文章站
2024-02-29 23:48:28
...
问题背景描述:公司的老项目,突然出现bug,以前运行好好的,代码也长时间未更新了,报错如下图:
报错的地方
然后改成相同子类遍历,不用子类转父类
还是报错,不是这个问题,然后发现语法基本没啥问题
然后对实体类进行分析,一直找不到问题
通过问题一步步排查,最后发现只要涉及到该实体类的类型转换,就会报错,不管是强转,还是自身调用转换都会出现报错
实体类ProjectUserBean子类
public class ProjectUserBean extends ProjectBean implements Serializable {
private static final long serialVersionUID = 1L;
protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private Integer userSignCount;
private Integer projectId;
private ProjectBean projectBean;
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public Integer getUserSignCount() {
return userSignCount;
}
public void setUserSignCount(Integer userSignCount) {
this.userSignCount = userSignCount;
}
public ProjectBean getProjectBean() {
return projectBean;
}
public void setProjectBean(ProjectBean projectBean) {
this.projectBean = projectBean;
}
}
实体类ProjectBean 父类
public class ProjectBean implements Serializable {
private static final long serialVersionUID = 1L;
protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private Integer projectId;
private String projectName;
private String createDime;
private String stage;
private String startDate;
private String endDate;
private Integer speed;
private Integer status;
private String projectManager;
private String managername;
private String dUserId;
// 总打卡人天
private Integer allSignCount;
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getCreateDime() {
return createDime;
}
public void setCreateDime(String createDime) {
this.createDime = createDime;
}
public String getStage() {
return stage;
}
public void setStage(String stage) {
this.stage = stage;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public Integer getSpeed() {
return speed;
}
public void setSpeed(Integer speed) {
this.speed = speed;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getProjectManager() {
return projectManager;
}
public void setProjectManager(String projectManager) {
this.projectManager = projectManager;
}
public String getdUserId() {
return dUserId;
}
public void setdUserId(String dUserId) {
this.dUserId = dUserId;
}
public String getManagername() {
return managername;
}
public void setManagername(String managername) {
this.managername = managername;
}
public Integer getAllSignCount() {
return allSignCount;
}
public void setAllSignCount(Integer allSignCount) {
this.allSignCount = allSignCount;
}
public static ProjectBean toBean(ProjectEntity peEntity) {
ProjectBean pb = new ProjectBean();
pb.setProjectId(peEntity.getId());
pb.setProjectName(peEntity.getProjectName());
pb.setStartDate(sdf.format(peEntity.getStartDate()));
pb.setEndDate(sdf.format(peEntity.getEndDate()));
pb.setStage(Constant.stages[peEntity.getStage()]);
pb.setSpeed(peEntity.getSpeed());
pb.setCreateDime(sdf.format(peEntity.getCreatetime()));
if(StringUtils.isNotEmpty(peEntity.getProjectManager())) {
RpcUserEntity rpcUser = RpcUserUtil.getRpcUserBean(peEntity.getProjectManager()).getRpcUser();
if(null != rpcUser) {
pb.setManagername(rpcUser.getNikename());
}
}
pb.setStatus(peEntity.getStatus());
pb.setProjectManager(peEntity.getProjectManager());
return pb;
}
}
个人觉得应该是实体类这里有问题,应该是无法判断类的类型所致,所以加上判断,如下:
for (Object userpb : pbs) {
if (userpb instanceof ProjectUserBean) {
ProjectUserBean projectBean= (ProjectUserBean) userpb;
set.add(projectBean.getProjectId());
}
}
最后验证发现果然是这样的