我想从其他bean中填充bean。
Example:
// this is mapped to db using hibernate.
class A {
string name;
string age;
Date dateA;
B obj;
}
// this was mapped to db but now I'd like to populate it from class A member dateA;
class B{
Date date;
}当我尝试设置B对象时,我得到了nullpointerexception。你知道怎么处理这个问题吗?
发布于 2012-09-12 22:19:53
Dozer是Java Bean到Java Bean的映射器,它递归地将数据从一个对象复制到另一个对象。
Mapper mapper = new DozerBeanMapper();
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);有关更多信息,请访问Dozer。
发布于 2012-09-12 22:27:35
您应该在调用obj.setDate()之前实例化B Obj = new B()。
如果你已经这样做了,如果我遗漏了什么,请在问题中提供足够的信息。
发布于 2012-09-12 22:28:33
Apache Commons BeanUtils有几种不同的方法来实现您的目标。
您可以使用BeanUtils.copyProperties()。还有BeanUtils.cloneBean()。
https://stackoverflow.com/questions/12390099
复制相似问题