po类中id有值,但是使用BeanUtils.copyProperties()拷贝出的vo类id属性为null,检查后发现是因为po继承的父类声明了一个泛型,部分代码如下:
public abstract class AbstractEntity<ID extends Serializable> implements Serializable {
protected ID id;
/**创建人*/
protected ID createdBy;
/**创建时间*/
protected Date createdTime;
/**最后一次修改人*/
protected ID lastModifiedBy;
/**最后一次修改时间*/
protected Date lastModifiedTime;
public void setId(ID id) {
this.id = id;
}
public ID getId() {
return this.id;
}
查看BeanUtils.copyProperties()源码中有一段判断如下:
if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType()))
当执行到获取vo类的writeMethod即setId()参数类型,结果是Long类型,而po类的readMethod即getId()返回值类型获取到的结果却是Serializable所以BeanUtils认为属性类型不同,所以不会拷贝id属性。
解决方法:暂不清楚po类extends AbstractEntity<Long>后为什么读取到的类型不是Long而是父类型Serializable,
暂时先不用泛型,把id类型直接定义为Long,问题解决