I am failed to understand the following problem.
I have one DTO file as follows.ClientRegistrationDTO.java
public class ClientRegistrationDTO {
private Long clientId;
private String clientCode;
private String clientName;
private String url;
private String logo;
private Long languageId;
private String timeZone;
//contact details
private Set<Address> addresses;
private Set<ContactDetails> contactDetails;
public ClientRegistrationDTO(){}
public ClientRegistrationDTO(Set<Address> addressSet,Set<ContactDetails> contactDetails){
this.addresses = addressSet;
this.contactDetails = contactDetails;
}
}
So I have a method registerClient
which accept this DTO file to operate over the save client
as well as a user
. As follows
public ClientRagistrationResponce registerClient(
ClientRegistrationDTO clientRegistrationDTO) {
ClientRagistrationResponce clientInfo = null;
if (clientRegistrationDTO != null) {
clientInfo = new ClientRagistrationResponce(); // creating responce variable
//setting another ragisterDTO file with default value
ClientRegistrationDTO clientRegistrationDTO2 = new
Line no 9 ClientRegistrationDTO(clientRegistrationDTO.getAddresses(),clientRegistrationDTO.getContactDetails());
// creating addresses Set from clientRegistrationDTO to insert
// against user.
Line no 15 Set<Address> clientAddress = new HashSet<>(clientRegistrationDTO.getAddresses());
Line no 16 Set<ContactDetails> clientContactDetails = new HashSet<>(clientRegistrationDTO.getContactDetails());
Line no 18 Set<Address> userAddress = new HashSet<>(clientRegistrationDTO2.getAddresses());
Line no 19 Set<ContactDetails> userContactDetails = new HashSet<>(clientRegistrationDTO2.getContactDetails());
// Save the client
Client client = saveClient(clientRegistrationDTO);
// Save the contact info
ContactInfo clientContactInfo = saveContactInfo(
client.getClientId(), RDHCoreConstants.CONTACT_TYPE_CLIENT);
// Save client addresses
Line no 29 Set<Address> clientAddressSet = saveClientAddresses(
clientContactInfo.getContactInfoId(), clientAddress);
// Save Client contact details
Line no 33 Set<ContactDetails> clientContactDetailsSet = saveClientContactDetails(
clientContactInfo.getContactInfoId(), clientContactDetails);
// save user
User user = saveUser(clientRegistrationDTO);
// Save User contact info
ContactInfo userContactInfo = saveContactInfo(user.getUserId(),
RDHCoreConstants.CONTACT_TYPE_USER);
// Save User addresses
saveUserAddresses(userContactInfo.getContactInfoId(), userAddress);
// Save User contact details
saveUserContactDetails(userContactInfo.getContactInfoId(),
userContactDetails);
saveClientUser(client.getClientId(), user.getUserId());
// setting the return DTO
clientInfo.setClientId(client.getClientId());
clientInfo.setAddresses(clientAddressSet);
clientInfo.setContactDetails(clientContactDetailsSet);
}
return clientInfo;
}
Firstly I created another Object with a different name of the same type to set address & contact details are safe with updation. @Line no 9.
So now whenever the client Address & ContactDetails saved @ line no 29 & 33.
it updates the address in ClientRagistrationDTO.java
file and all the reference which I passed to create another set of Address and contactDetails. @ Line no 9.
I'm confused, why it's updating all its reference, even though I never updated the same.
Please guide me step by steps.
Note: I used Spring Data JPA with following relation ContactInfo
table has a contactInfoId
as primary_key. whihc is foreign_key in Address
as well as ContactDetails
.
Follwing is the entity of the tables.ContactInfo
@Entity
@Table(name = "contactinfo", schema = "test")
public @Data class ContactInfo {
@Id
@Column(name = "CONTACTINFOID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long contactInfoId;
@Column(name="CLIENTUSERID")
private Long clientUserId;
@Column(name = "CONTACTTYPE")
@NotNull
@Size(min = 1, max = 10)
private String contactType;
}
Follwing is the Address
@Entity
@Table(name = "address", schema = "test")
public class Address {
@Id
@Column(name = "ADDRESSID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long addressId;
@Column(name = "ADDRESS1")
@Size(min = 1, max = 50)
private String address1;
@Column(name = "ADDRESS2")
@Size(min = 1, max = 50)
private String address2;
@Column(name = "CITY")
@Size(min = 1, max = 50)
private String city;
@Column(name = "ZIP")
@Size(min = 1, max = 15)
private String zip;
@Column(name = "STATE")
@Size(min = 1, max = 50)
private String state;
@Column(name = "COUNTRY")
@Size(min = 1, max = 50)
private String country;
@Column(name = "ISPRIMARY")
@NotNull
@Size(min = 1, max = 1)
private String isPrimary;
@Column(name = "CONTACTINFOID")
private Long contactInfoId;
}
Follwing is the ContactDetails.
@Entity
@Table(name = "contactdetails", schema = "test")
public class ContactDetails {
@Id
@Column(name = "CONTACTDETAILSID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long contactId;
@Column(name = "CONTACTTYPE")
@NotNull
@Size(min = 1, max = 20)
private String contactType;
@Column(name = "CONTACTDETAIL")
@NotNull
@Size(min = 1, max = 20)
private String contactDetail;
@Column(name = "EXTENSION")
private String extension;
@Column(name = "ISPRIMARY")
@NotNull
@Size(min = 1, max = 1)
private String isPrimary;
@Column(name = "CONTACTINFOID")
private Long contactInfoId;
}
Hope enough information is provided to solve this issue.