In JPA entity you can annotate fields or methods (getters). The @Id
annotation dictates this, meaning if you put @Id
on a field then all your annotations should go on fields but if you put it on, for example, @Id Long getId()
then other annotations should follow. That's why @Transient
can be on a method as well.
For example, if you have this
@Id
private Long id;
@Transient
private String someTransientField;
private Long getId() {
return this.id;
}
private String getSomeTransientField() {
return this.someTransientField;
}
then someTransientField
would be treated as transient. But if @Id
would stay on the field, and you move @Transient
to private String getSomeTransientField()
then someTransientField
would be treated as persistent, since @Id
is on the field and therefore all other annotations are expected to be on fields as well.
So the case where @Transient
would work on the method is this
private Long id;
private String someTransientField;
@Id
private Long getId() {
return this.id;
}
@Transient
private String getSomeTransientField() {
return this.someTransientField;
}