0

我想从其他 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。知道如何处理这个问题吗?

4

4 回答 4

1

Dozer 是一个 Java Bean 到 Java Bean 的映射器,它递归地将数据从一个对象复制到另一个对象。

Mapper mapper = new DozerBeanMapper();
DestinationObject destObject =  mapper.map(sourceObject, DestinationObject.class);

有关更多信息,请关注 推土机

于 2012-09-12T14:19:52.873 回答
0

进行这种映射的一个非常流行的库是Apache commons BeanUtils

它具有将一个 bean 属性复制到另一个的全面功能。

您可以在此处此处查看使用示例。

于 2012-09-12T14:36:51.807 回答
0

You should instantiate B Obj = new B() before invoking obj.setDate() .

If you are already doing this, If i am missing something please provide sufficient info in the question.

于 2012-09-12T14:27:35.413 回答
0

Apache Commons BeanUtils has several different methods to accomplish your goal.

You could use BeanUtils.copyProperties(). There is also BeanUtils.cloneBean().

于 2012-09-12T14:28:32.617 回答