Method 1:
"Cloning with spring comes with Beanutils"
"Requirement: the class that needs to be cloned implements the Cloneable interface and overrides the Clone () method"
Example
"Entity:
Packagecom.agen.orderdiscount.entity;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportLombok. Noargsconstructor;Importlombok.experimental.Accessors;ImportOrg.hibernate.annotations.GenericGenerator;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;Importjava.util.Date;/*** Estimated amount * The order creation was successfully credited to the estimate amount table *@authorSXD * @date 2018/1/16*/@Data (StaticConstructor= "of") @NoArgsConstructor @allargsconstructor@accessors (Chain=true) @Entity @genericgenerator (name= "Uuid2", strategy = "Org.hibernate.id.UUIDGenerator" ) Public classEsamountImplementscloneable{/*** Estimated ID*/@Id @GeneratedValue (Generator= "Uuid2") @Column (length= 36) PrivateString Id; /*** The estimated amount of this transfer*/@Column (Nullable=false, precision = 10,scale = 2) PrivateDouble Esaccount; /*** This time of transfer*/@Column (Nullable=false) PrivateDate esdate; /*** Order ID*/@Column (Nullable=false) PrivateInteger orderId; /*** Order number SN*/@Column (Nullable=false, length = 20) PrivateString ORDERSN; /*** Agency ID*/@Column (Nullable=false) PrivateInteger Adminid; /*** Product ID*/@Column (Nullable=false) PrivateInteger productId; /*** Member ID*/@Column (Nullable=false) PrivateInteger MemberID; /*** Sample Package ID*/@Column (Nullable=false) PrivateInteger cybid; /*** Operating Source*/@Column (Nullable=false, length = 20) PrivateString Esoperater; /*** Estimated amount remark 1*/@Column (Length= 500) PrivateString EsCre1; /*** Estimated amount remark 2*/@Column (Length= 500) PrivateString EsCre2; @OverrideprotectedObject Clone ()throwsclonenotsupportedexception {return Super. Clone (); }}
View Code
"Use:
Esamount Esamount = Esamountrepository.findesamountbyadminidandorderidandproductid (adminId,orderId,productId) ; if (Objects.nonnull (Esamount)) { //1.1) The record of deduction of the estimated amount to the deductible amount is also recorded in the estimated Amount record table in the new Esamount (); Beanutils.copyproperties (Esamount1,esamount); if (Objects.nonnull (ESAMOUNT1)) {
View Code
Beanutils.copyproperties (Esamount1,esamount); Clone parameter 2 to parameter 1
=====================================================================================
Method 2:
"Using java.lang.Serializable for deep cloning"
"Requirements, do not need classes to implement cloneable, just need them to implement java.lang.Serializable."
"Tool class:
ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream; Public Abstract classbeanutil {@SuppressWarnings ("Unchecked") Public Static<T> t Cloneto (t SRC)throwsruntimeexception {bytearrayoutputstream Memorybuffer=NewBytearrayoutputstream (); ObjectOutputStream out=NULL; ObjectInputStream in=NULL; T Dist=NULL; Try{ out=NewObjectOutputStream (Memorybuffer); Out.writeobject (SRC); Out.flush (); Inch=NewObjectInputStream (NewBytearrayinputstream (Memorybuffer.tobytearray ())); Dist=(T) in.readobject (); } Catch(Exception e) {Throw NewRuntimeException (e); } finally { if(Out! =NULL) Try{out.close (); out=NULL; } Catch(IOException e) {Throw NewRuntimeException (e); } if(In! =NULL) Try{in.close (); Inch=NULL; } Catch(IOException e) {Throw NewRuntimeException (e); } } returnDist; }}
View Code
Use
New Administrator (newnewtrue); = Beanutil.cloneto (SRC);
View Code
=====================================================================================
Cloning of entity in the "Java" Spring project between classes