Using constructor and using set() for same attributes

Asked

Viewed 593 times

6

I’m studying JPA following the example of uaiContacts.

His project is on Github.

I took the file example modelContact.java:

The class Contact is mapped:

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

and she uses a builder.

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

He uses set.

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

Maybe I shouldn’t just use get?

Remarks:

No inheritance to initialize the constructor; Soon I didn’t understand the super(), since it has no parent class or perhaps some other project class extend this class.

Full model contact. model/Contact.java class code

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 on github:

https://github.com/uaihebert/uaicontacts

  • Hello! Preferably put also the whole class of Contact (or a link to it), so we can see the gets and sets and opine on top.

  • @Dheriki edited the post with the full class. I also passed the project link on github.

  • Is @Autowired a form of erance? Why does the service/Contactservice.java file have this line.

  • Really, in class Contact there is the empty constructor, as anticipated in the answer. The @Autowired is from spring, for injection of dependency of Repository in its service class, has no relation to the talk about inheritance in JPA.

1 answer

4

Why does it use get and set? If the constructor already arrow such values when we define an object. [...] Maybe you shouldn’t just use get?

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

Otherwise, there would be no need set nor the get, because JPA requires neither one nor the other.

No inheritance to initialize the constructor super(), since it has no parent class or perhaps some other class of the project extend this class.

In this case, it’s really unnecessary.

Recalling that the JPA always demands a constructor without arguments.

  • @Skywalker, I saw it in your previous comment. I look forward to correcting my answer, but wanted to know if you have any link/documentation talking about it. In practice I can’t test it now. I asked for the class link Contact for André to see how this class is doing.

  • He said he’s not using inheritance in class Contact.

  • But the way you’re talking, I’m still gonna need a builder (protected) no arguments. I got it right?

  • Cool, @Skywalker. Thanks for the contribution too, I ended up remembering some things from JPA thanks to our conversation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.