Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Id

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primary key of an entity can be annotated on the field, on the getter. Is it 'valid' to annotate the setXXX( ) with the @Id?

@Id
private in id;

Or

@Id
public int getId() { }


@Id
public in setId( ) { } <- I remember having read somewhere that persistence provider ignores this! so this is not invalid, although not used?
 
Sameer Ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DI is done on setters using annotations but JPA uses getters for annotations.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JPA terms, annotating the setters are silently ignored by the container. It should be a good candidate for find bugs when you do so.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so all the three ways are perfectly valid candidates to be correct answers!
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JPA terms, it is not valid to annotate on a setter though you will be able to compile and run your code.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jothi. JPA spec 2.1.1 says:

When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors. These annotations must not be applied to the setter methods.

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic