javaspring项目中 对entity进行本类间的克隆

Posted Angel挤一挤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaspring项目中 对entity进行本类间的克隆相关的知识,希望对你有一定的参考价值。

方法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

 

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

以上是关于javaspring项目中 对entity进行本类间的克隆的主要内容,如果未能解决你的问题,请参考以下文章

hibernate 注解

如何在dotnet core Entity Framework中对多个表的数据进行GroupBy操作?

JavaSpring Boot项目的创建和使用

Javaspring MVC中定义异常页面

Entity Framework Core 7中高效地进行批量数据插入

JavaSpring中的注解