欢迎光临
感受代码之美

Java实现两个实体类相同属性的复制/拷贝

在两个实体类里,有相同的属性,有不同的属性,相同名称的属性类型还可能不同,如何实现相同属性的拷贝/复制呢?

实体类

代码清单1:Neo4jNode.java

package com.graph.domain;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:四个空格-https://www.4spaces.org
 */

@Data
public class Neo4jNode {
    private String id;

    private String label;

    private String labelName;

    private String typeUri;

    private Double x;

    private Double y;

    private Map<String, Object> properties = new HashMap<>();
}

代码清单2:SigmaNode.java

package com.graph.domain;

import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * @Author:四个空格-https://www.4spaces.org
 */

@Data
public class SigmaNode {

    private String id;

    private String label;

    private String labelName;

    private String typeUri;

    private Double x;

    private Double y;

    private List<Map<String, Object>> properties;
}

方式1:org.springframework.beans.BeanUtils

package com.graph.domain;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanCopyExample {
    public static void main(String[] args) {
        SigmaNode sigmaNode = new SigmaNode();
        sigmaNode.setId("1");
        sigmaNode.setLabel("https://www.4spaces.org");
        sigmaNode.setLabelName("四个空格");

        Map<String, Object> propertieMap = new HashMap<>();
        propertieMap.put("title", "博客");
        propertieMap.put("subject", "技术");
        List<Map<String, Object>> properties = new ArrayList<>();
        properties.add(propertieMap);

        sigmaNode.setProperties(properties);

        Neo4jNode neo4jNode = new Neo4jNode();
        BeanUtils.copyProperties(sigmaNode, neo4jNode);
        System.out.println(JSON.toJSONString(neo4jNode));

    }
}

方式2:org.apache.commons.beanutils.BeanUtils

package com.graph.domain;

import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanCopyExample {
    public static void main(String[] args) {
        SigmaNode sigmaNode = new SigmaNode();
        sigmaNode.setId("1");
        sigmaNode.setLabel("https://www.4spaces.org");
        sigmaNode.setLabelName("四个空格");

        Map<String, Object> propertieMap = new HashMap<>();
        propertieMap.put("title", "博客");
        propertieMap.put("subject", "技术");
        List<Map<String, Object>> properties = new ArrayList<>();
        properties.add(propertieMap);

        sigmaNode.setProperties(properties);

        Neo4jNode neo4jNode = new Neo4jNode();
        try {
            BeanUtils.copyProperties(sigmaNode, neo4jNode);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(neo4jNode));

    }
}

总结

由于两个实体类properties属性类型不一样,org.apache.commons.beanutils.BeanUtils会抛出:java.lang.IllegalArgumentException异常,而org.springframework.beans.BeanUtils则不会对该属性赋值,不会抛出异常。

参考文章:

  1. Copy POJO content from one bean to another

  2. org.springframework.beans.BeanUtils

  3. org.apache.commons.beanutils.BeanUtils

转载请注明来源:四个空格 » Java实现两个实体类相同属性的复制/拷贝

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址