1

In Hibernate, placing there @Id annotation before the field declaration itself

@Id
private int id;

and before its getter and setter

@Id
public int getId()  {  return this.id; }
public int setId(..) { .. }

are two different things. This difference takes effect if there's some processing in getter/setter.

The Q is, is there such an issue for non-key fields-- those without the @Id annotation?

My key fields aren't any processed. However, among the others, I've got some fields that i am validating/changing the values before setting. For those fields, should i put the annotations before their getters-setters?

I didn't hit a brick wall as far as I could observe. however - would like to make sure.

TIA.

Note: saw the useful discussion: Where to put hibernate annotations?.

Community
  • 1
  • 1
Roam
  • 4,831
  • 9
  • 43
  • 72

1 Answers1

0

Well no issue for non-key fields, but personally I prefer fields annotation, expecially if you don't have to do some business logic on entity level: code is cleaner, all db related stuff is at the beginning of the class, you don't have strange issue with equals methods (which always use fields - it happened to me) plus, if you have other methods not strictly related to db handling you have to set them @Transient.

Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51