码迷,mamicode.com
首页 > 编程语言 > 详细

【java】spring项目中 对entity进行本类间的克隆

时间:2018-01-19 15:48:35      阅读:808      评论:0      收藏:0      [点我收藏+]

标签:cep   gif   nts   ons   记录   b16   product   lan   nullable   

方法1:

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

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

》例子:

》》实体:

技术分享图片
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

》》使用:

技术分享图片
 EsAmount esAmount = esAmountRepository.findEsAmountByAdminIdAndOrderIdAndProductId(adminId,orderId,productId);
                                if(Objects.nonNull(esAmount)){
                                    //1.1 将预估金额扣除到可提现金额的记录也记录到预估金额记录表中
                                    EsAmount esAmount1 = new EsAmount();
                                    BeanUtils.copyProperties(esAmount1,esAmount);
                                    if(Objects.nonNull(esAmount1)){
View Code
BeanUtils.copyProperties(esAmount1,esAmount);  将参数2克隆一份给参数1

 

 

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

方法2:

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

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

》工具类:

技术分享图片
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

》使用:

技术分享图片
Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);
        Administrator dist = BeanUtil.cloneTo(src);
View Code

 

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

【java】spring项目中 对entity进行本类间的克隆

标签:cep   gif   nts   ons   记录   b16   product   lan   nullable   

原文地址:https://www.cnblogs.com/sxdcgaq8080/p/8316960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!