Cloning of Address in Hybris
We know that every customer in ecommerce site will have address associated with them.
We also know that cart and order will also have an address which is same as customer address
Cart will have a delivery address which is always referencing the address owned by a Customer
So, whenever customer edits the address, same address will be updated for cart delivery address.
This is because, cart delivery address is obtained by shallow cloning.
Once the cart is converted to an order, the cart address will be cloned and attached to the newly created order.
cloned address in order is owned by the order and not by the customer which means it does deep cloning.
How to clone address in Hybris?
We can clone address in either of the following 2 ways
1) Shallow cloning
2) Deep cloning
1) Shallow cloning
If we want to clone address and store only reference of the original address then we can do shallow cloning as below
- AddressModel clonedAddress = modelService.clone(originalAddress,AddressModel.class);
AddressModel clonedAddress = modelService.clone(originalAddress,AddressModel.class);
In this case, any modification to original address will be reflected in cloned address
Example : Cart delivery address cloned from customer address
2) Deep cloning
If we want to clone address and store it independent of original address then we can do deep cloning as below
- AddressModel clonedAddress = addressService.cloneAddress(originalAddress);
AddressModel clonedAddress = addressService.cloneAddress(originalAddress);
Where addressService is an instance of AddressService class
In this case, any modification to original address will not be reflected in cloned address
Example : order delivery address cloned from cart delivery address
Note : One Customer can have any number of addresses.
Thanks for the easy to understand details.
But what would happen if we clone the original address.
AddressModel clonedAddress = modelService.clone(originalAddress,AddressModel.class);
modelService.remove(originalAddress);
clonedAddres.setSomeAttribute(…); //will this work?
Does the cloned address get effected as it is referencing a removed object now.