0

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.

  • It works the same as in previous versions. If you're having trouble understanding how references work, maybe take a look at https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 – Kayaman Jul 05 '18 at 11:21
  • What a lot of code. Could you instead [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), please? At least I am not going to read through your question as it stands. – Ole V.V. Jul 05 '18 at 11:22
  • @OleV.V. you don't need to read all code just read the paragraph or text. you will understand my question. – MANOHAR SINGH RAGHUWANSHI Jul 05 '18 at 11:31
  • @Kayaman . yeah i know its pass by value. Then why it updating the reference variable whihc I already created before the operation. even though its not updated in code. – MANOHAR SINGH RAGHUWANSHI Jul 05 '18 at 11:33
  • 1
    If it's getting updated, then it **is** updated in code. The alternative would be a cursed JVM. The question is too big and unclear, but I'll assume you're sharing the same collection and when it gets updated you're surprised that what you thought were separate collections are actually the same one. – Kayaman Jul 05 '18 at 11:38
  • @Kayaman : thanks , somewhat get it – MANOHAR SINGH RAGHUWANSHI Jul 05 '18 at 14:09
  • Correct me if I’m wrong, but isn’t the entire purpose of JPA to sync data with a database? So object identities do not matter, but data base IDs. – Holger Jul 05 '18 at 14:35
  • @Holger : i didn't get you. Please explain clearly. Sorry as I said I am beginner . – MANOHAR SINGH RAGHUWANSHI Jul 05 '18 at 14:49
  • @Kayaman : I chaged the collection also for setting and getting. Still not able to protect my data. – MANOHAR SINGH RAGHUWANSHI Jul 05 '18 at 14:50
  • 1
    Really? Why do beginners start with business applications using JPA? Do you understand what JPA is for and/or why you are using it? When it stores an object to the database, the ID matters, which, from Java’s point of view is just a property. When it restores an object from the database, again the ID matters, which still is just a property from Java’s point of view. When you have two distinct Java objects with the same property value for what the database considers to be the ID, they will “magically” sync their other properties via the database. You have to care to assign unique IDs. – Holger Jul 05 '18 at 14:55
  • @Holger : thank you so much for your clear explanation. I imagine your pain to explain it. but anyways thanks a lot. :) – MANOHAR SINGH RAGHUWANSHI Jul 06 '18 at 05:23

0 Answers0