java - javax.persistence Annotations on field, getter or setter? -
java - javax.persistence Annotations on field, getter or setter? -
i learning hibernate , java persistence api.
i have @entity class, , need apply annotations various fields. have included in code below 3 places go.
should apply them field itself, getter or setter? , semantic difference, if any, between these 3 options.
import javax.persistence.entity; import javax.persistence.table; import javax.persistence.id; @entity @table(name = "song") public class song { // annotations should applied 1 of below @id @column(name="id", unique=true, nullable=false) private int id; @id @column(name="id", unique=true, nullable=false) public int getid() { homecoming id; } @id @column(name="id", unique=true, nullable=false) public void setid(int id) { this.id = id; } }
you have take between field , getter. annotations on setters not supported. , annotations should on fields, or should on getters: can't mix both approaches (except if utilize @accesstype
annotation).
regarding 1 preferrale, reply is: depends. prefer field access, ymmv, , there situations property access preferrable. see hibernate annotations - better, field or property access?.
java hibernate-mapping
Comments
Post a Comment