2

I am working on JPA and JSF.

I found two ways to put the annotation:

1. Above the instance variable.

public class Project implements Serializable {
    @ID
    @NotNull
    private Long idProject
    public Long getIdProject() {
        return this.idProject
    }
}

2. Above getter

public class Project implements Serializable {
    private Long idProject
    @ID
    @NotNull
    public Long getIdProject() {
        return this.idProject}
    }

What is the difference? What is the best to use?

2 Answers2

1

This question is so popular and sooo cloned. Dont wanna make another clone :)

the difference between anotating a field and its getter method JPA

Community
  • 1
  • 1
0

Like Sotirios said, both are supported but I much prefer putting the Annotations on the variable. The getters and setters are usually at the end of the class (at least that's where "I" think they belong). If you don't Annotate at the variable then you leave the maintainer to go search and make sure you wired it in "somewhere". I haven't heard a good reason why you would put it on the setter method.

Terry
  • 911
  • 10
  • 26