优米格
分享有营养的

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

BraUndress.png

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

实体类

代码清单1:Neo4jNode.java

package com.graph.domain;

import lombok.Data;

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

/**
 * @Author:四个空格-https://www.yomige.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.yomige.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.yomige.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.yomige.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

赞(0)
未经允许禁止转载:优米格 » Java实现两个实体类相同属性的复制/拷贝

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系