方法1:

【使用spring自带BeanUtils实现克隆】

【要求:需要被克隆的类实现Cloneable接口并且重写clone()方法】

》例子:

》》实体:

【java】spring项目中 对entity进行本类间的克隆_深度克隆【java】spring项目中 对entity进行本类间的克隆_深度克隆_02
package com.agen.orderdiscount.entity;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;

/**
 * 预估金额
 * 订单创建成功 往预估金额表中存入记录
 * @author SXD
 * @date 2018/1/16
 */
@Data(staticConstructor = "of")
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator" )
public class EsAmount implements Cloneable{

    /**
     * 预估ID
     */
    @Id
    @GeneratedValue(generator = "uuid2")
    @Column(length = 36)
    private String Id;
    /**
     * 本次转入预估金额
     */
    @Column(nullable = false,precision = 10,scale = 2)
    private Double esAccount;
    /**
     * 本次转入时间
     */
    @Column(nullable = false)
    private Date esDate;
    /**
     * 订单ID
     */
    @Column(nullable = false)
    private Integer orderId;
    /**
     * 订单编号SN
     */
    @Column(nullable = false,length = 20)
    private String orderSn;
    /**
     * 机构ID
     */
    @Column(nullable = false)
    private Integer adminId;
    /**
     * 产品ID
     */
    @Column(nullable = false)
    private Integer productId;
    /**
     * 会员ID
     */
    @Column(nullable = false)
    private Integer memberId;
    /**
     * 采样包ID
     */
    @Column(nullable = false)
    private Integer cybId;
    /**
     * 操作来源
     */
    @Column(nullable = false,length = 20)
    private String esOperater;
    /**
     * 预估金额备注1
     */
    @Column(length = 500)
    private String esCre1;
    /**
     * 预估金额备注2
     */
    @Column(length = 500)
    private String esCre2;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }




}
View Code

》》使用:

【java】spring项目中 对entity进行本类间的克隆_深度克隆【java】spring项目中 对entity进行本类间的克隆_深度克隆_02
EsAmount esAmount = esAmountRepository.findEsAmountByAdminIdAndOrderIdAndProductId(adminId,orderId,productId);
                                if(Objects.nonNull(esAmount)){
                                    //1.1 将预估金额扣除到可提现金额的记录也记录到预估金额记录表中
                                    EsAmount esAmount1 = new EsAmount();
                                    BeanUtils.copyProperties(esAmount,esAmount1);
                                    if(Objects.nonNull(esAmount1)){
View Code
BeanUtils.copyProperties(esAmount,esAmount1);  将参数1克隆一份给参数2

 

 

=====================================================================================

方法2:

【使用java.lang.Serializable实现深度克隆】

【要求,不需要类实现Cloneable,只需要它们实现java.lang.Serializable即可】

》工具类:

【java】spring项目中 对entity进行本类间的克隆_深度克隆【java】spring项目中 对entity进行本类间的克隆_深度克隆_02
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public abstract class BeanUtil {
    @SuppressWarnings("unchecked")
    public static <T> T cloneTo(T src) throws RuntimeException {
        ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        T dist = null;
        try {
            out = new ObjectOutputStream(memoryBuffer);
            out.writeObject(src);
            out.flush();
            in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));
            dist = (T) in.readObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null)
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            if (in != null)
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }
        return dist;
    }
}
View Code

》使用:

【java】spring项目中 对entity进行本类间的克隆_深度克隆【java】spring项目中 对entity进行本类间的克隆_深度克隆_02
Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);
        Administrator dist = BeanUtil.cloneTo(src);
View Code

 

=====================================================================================