I have an experience with updating a user profile with checking unique email and phone number. In that case I want to get my previous profile details and want to set new updated object details. But after I set the new object details to my retrieved previous object, my previous object setting with new data. So I have a way to again recall the previous data object using a database call.
But if you prefer to maintain a performance based app, you can reduce the database call using cloning the objects. Here actually we create the same object as old and new two objects.
First I will show how we can achieve using Java 8 version.
Think if you have two classes Profile and DomainEntity. Here actually DomainEntity used for extend generic objects like created date, updated date etc.
@Getter
@Setter
public class DomainEntity {
private Date createdDate;
private Date updatedDate;
private boolean deleted;
}
@Getter
@Setter
public class Profile extends DomainEntity {
private String firstName;
private String lastName;
private String address;
private Integer age;
}
With these classes you can see I used domain entity to share my generic objects to my profile class.
So, here is the trick goes. I will create a constructor and pass same object as a parameter to that constructor. I do the same thing for both child and parent classes.
Profile(Profile profile) {
if (profile != null) {
this.firstName = profile.firstName;
this.lastName = profile.lastName;
this.address = profile.address;
this.age = profile.age;
}
}
for the domain entity class, I used the same way.
So, the final class with be looks like below.
@Getter
@Setter
public class Profile extends DomainEntity {
private String firstName;
private String lastName;
private String address;
private Integer age;
Profile(Profile profile) {
super(profile);
if (profile != null) {
this.firstName = profile.firstName;
this.lastName = profile.lastName;
this.address = profile.address;
this.age = profile.age;
}
}
}
@Getter
@Setter
public class DomainEntity {
private Date createdDate = new Date();
private Date updatedDate = new Date();
private boolean deleted;
DomainEntity(DomainEntity entity) {
if (entity != null) {
this.createdDate = entity.createdDate;
this.updatedDate = entity.updatedDate;
this.deleted = entity.deleted;
}
}
}
Lets see how we can use this objects in our codebase.
Profile profile = this.profileRepository.findById(id);
// copy the profile object
Profile previousProfile = new Profile(profile);
// update the existing profile object
profile.setFirstName("New First Name");
profile.setLastName("New Last Name");
Now you can get the old object details from the previousProfile and new object details details using profile object.
This is how we create clone objects in Java. There is another way we can use to clone objects.
Profile profile = SerializationUtils.clone(profile);
This utility might not available with Java 8 and old versions. But the latest java versions should available this inbuilt method.
I think you will get an idea about how to cloning the Java objects.
Have a nice day you all.....
Comments
Post a Comment