I am new to Spring Boot and JPA in general. I've seen examples of adding JPA annotations on field declarations such as this:
@Entity
public class Fizz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// other fields
public Fizz(Long id) {
super();
setId(id);
}
// setter defined here
public Long getId() {
return this.id;
}
}
...as well as examples putting the same annotations on the getters like this:
@Entity
public class Fizz {
private Long id;
// other fields
public Fizz(Long id) {
super();
setId(id);
}
// setter defined here
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return this.id;
}
}
I'm wondering if they are semantically equivalent or if there are different use cases where you'd choose one over the other. I ask because I'm actually writing my Spring Boot/JPA app in Groovy where you typically don't define getters:
@Canonical
@Entity
class Fizz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id
}