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:
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.– Dherik
@Dheriki edited the post with the full class. I also passed the project link on github.
– André Nascimento
Is @Autowired a form of erance? Why does the service/Contactservice.java file have this line.
– André Nascimento
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.– Dherik