Using constructor and using set () for same attributes

6

I am studying JPA following the example of uaiContacts .

His project is in GitHub.

I took the example of the modelContact.java file:

The Contact class is mapped:

public class Contact {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String phoneNumber;
    private String email;
}

and it uses a constructor.

public Contact(String name, String phoneNumber, String email, int id) {
    super();
    this.name = name;
    this.phoneNumber = phoneNumber;
    this.email = email;
}

It uses set .

The question is: Why does it get get and set ? If the constructor already sets these values when defining an object.

Maybe you should not only use get ?

Comments:

Has no inheritance to initialize the constructor; So I do not understand super() , since it has no parent class or maybe some other project class extends this class.

Complete model contact. model / Contact.java

package uaiContacts.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Contact {

    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String phoneNumber;
    private String email;

    public Contact(){

    }

    public Contact(String name, String phoneNumber, String email, int id) {
        super();
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

@Override
    public boolean equals(Object object) {
        if (object instanceof Contact){
            Contact contact = (Contact) object;
            return contact.id == id;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return id;
    }
}

Take, if you prefer, the code in gitHub:

link

    
asked by anonymous 28.08.2015 / 16:51

1 answer

4
  

Why does it use get and set? If the constructor already indicates such values when   we define an object. [...] Maybe I should not just use get?

The set is for updating the entity information, since it already uses the constructor to instantiate entity information. The get is for the information to become available.

Otherwise, there would be no need for set nor get , as JPA does not require either.

  

Has no inheritance to initialize the constructor; I soon did not understand the   super (), since it has no parent class or maybe some other class of the   project extends this class.

In this case, it is unnecessary.

Remembering that JPA always requires a constructor with no arguments.

    
28.08.2015 / 17:01