简体   繁体   English

JAVA - 如何将对象的属性复制到具有相同属性的另一个对象?

[英]JAVA - How to copy attributes of an object to another object having the same attributes?

Let's say we have an Object A defined like this: 假设我们有一个像这样定义的Object A:

public class ObjectA {
    private Attribute a1;
    private Attribute a2;
    private Attribute a3;
}

For some reason, I need to create a second object B with only the first two attriutes of the Object A : 出于某种原因,我需要创建第二个对象B,只有对象A的前两个属性:

public class ObjectB {
    private Attribute a1;
    private Attribute a2;
}

So my question is: what is the best approach to copy an Object A to an Object B ? 所以我的问题是:将对象A复制到对象B的最佳方法是什么? I've been copying the attributes by getters and setters one by one but something tells me there must be a better way to do this ! 我一直在通过getter和setter来复制属性,但有些东西告诉我必须有更好的方法来做到这一点! Especially when the object will have a lot of attributes, I have to write lines and lines of code just to copy all of them to the second Object B ... 特别是当对象具有很多属性时,我必须编写行和代码行才能将它们全部复制到第二个对象B ...

Thanks a lot :) 非常感谢 :)

EDIT: I've been being alerted by a "possible duplicate of another question" : How do I copy an object in Java? 编辑:我一直被“另一个问题的可能重复”警告:我如何用Java复制一个对象?

My question is slightly different in a way that I'm dealing with 2 different objects who just share the same attributes but not totally ! 我的问题略有不同,我正在处理两个不同的对象,这些对象只是共享相同的属性但不完全!

尝试使用DozerBeanUtils等库

To expand on my comment: 扩展我的评论:

Using Dozer it can be as easy as: 使用Dozer可以很简单:

Mapper mapper = new DozerBeanMapper();
ObjectA source = new ObjectA();
ObjectB target = mapper.map(source , ObjectB.class);

or if your target class doesn't have a no-arg constructor: 或者如果您的目标类没有no-arg构造函数:

ObjectA source = new ObjectA();
ObjectB target = new ObjectB(/*args*/);
mapper.map(source, target );

From the Documentation (emphasis by me): 从文档(我强调):

After performing the Dozer mapping, the result will be a new instance of the destination object that contains values for all fields that have the same field name as the source object . 执行Dozer映射后,结果将是目标对象的新实例,该实例包含与源对象具有相同字段名称的所有字段的值 If any of the mapped attributes are of different data types, the Dozer mapping engine will automatically perform data type conversion. 如果任何映射的属性具有不同的数据类型,则Dozer映射引擎将自动执行数据类型转换。

What you need is Object mappers. 你需要的是对象映射器。 Try Orika or Dozer . 尝试OrikaDozer The objects need not be of the same type. 对象不必是相同类型。 While mapping if it finds the attributes of the same name and type, it automatically maps it. 如果映射找到相同名称和类型的属性,则会自动映射它。

MapperFacade mapper = mapperFactory.getMapperFacade();
UserDTO userDTO = new UserDTO();
userDTO.setName("xyz");
..
User user = mapper.map(userDTO, User.class);

You can also customize if you have to map different attribute names. 您还可以自定义是否必须映射不同的属性名称。

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(UserDTO.class, User.class)
            .field("name", "username")
            .byDefault().register();
mapper = mapperFactory.getMapperFacade();
...
User user = mapper.map(userDTO, User.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM