| Author |
How to write Field baed access entity
|
Karnati Sudhakar
Ranch Hand
Joined: Aug 26, 2008
Posts: 270
|
|
Hi All,
I am just wondering if the above code for the entity class is using field based access correctly or should i add anything else for field based access. In property based access we can do like this:
employee.setEmployeeName(name);
but in field based access how to do that? Regards Sudhakar [ November 25, 2008: Message edited by: Karnati Sudhakar ]
|
Regards
Sudhakar
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
for using field based access, your code need's to be modified here
@Enumerated(EnumType.STRING) protected Employeetype emptype; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; protected String EmployeeName; @Temporal(TemporalType.TIMESTAMP) protected Date creationdate;
@Column @Enumerated(EnumType.STRING) protected Employeetype emptype; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column protected String EmployeeName; @Column @Temporal(TemporalType.TIMESTAMP) protected Date creationdate; I have added this annotation @Column to the protected field's which will be as column's. [ November 25, 2008: Message edited by: Amandeep Singh ]
|
SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
|
 |
Karnati Sudhakar
Ranch Hand
Joined: Aug 26, 2008
Posts: 270
|
|
Aman, Thanks for your reply. I have added @Column annotation to the fields but i am getting warning like:
Persistent attribute should be encapsulated(have getter and setter)
Can any body tell me how to modify the fields in field based access. Regards Sudhakar
|
 |
Ralph Jaus
Ranch Hand
Joined: Apr 27, 2008
Posts: 342
|
|
The jpa spec says (2.1):
The persistent state of an entity is represented by instance variables, which may correspond to JavaBeans properties. An instance variable may be directly accessed only from within the methods of the entity or by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity's accessor methods (getter/setter methods) or other business methods. Instance variables must be private, protected, or package visible.
|
SCJP 5 (98%) - SCBCD 5 (98%)
|
 |
Karnati Sudhakar
Ranch Hand
Joined: Aug 26, 2008
Posts: 270
|
|
Thanks Ralph for pointing me to the spec.
The persistent state of an entity is represented by instance variables, which may correspond to JavaBeans properties. An instance variable may be directly accessed only from within the methods of the entity or by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity's accessor methods (getter/setter methods) or other business methods. Instance variables must be private, protected, or package visible.
What is the use of field based access? if we can only update fields from Entity itself.
I have added this annotation @Column to the protected field's which will be as column's.
Just having @Column annotation makes entity as Field based access? Regards Sudhakar [ November 26, 2008: Message edited by: Karnati Sudhakar ]
|
 |
Ralph Jaus
Ranch Hand
Joined: Apr 27, 2008
Posts: 342
|
|
What is the use of field based access? if we can only update fields from Entity itself.
Good question. Actually, in most cases the spec will force you to write setter and getter methods.
Just having @Column annotation makes entity as Field based access?
No. "Field based" means that @Id is used on field level (in accordance other annotations like @Column must be used on field level, too). "Propertiy based" access means that @Id is used on the getter method of the corresponding field (in accordance other annotations like @Column must be used on getter methods, too). Example: - Field based access: @Id private long id; - Property based access: private int id; ... @Id public int getId(){ return id; } "Access" describes here the way how the persistence provider accesses the instance variables: If you use field based access, the persistence provider will assign an object/value (that it retieved from a jdbc call) directly to the instance variable. Setter methods are ignored by the persistence provider in this case. If you use property based access the persistence provider will call the corresponding setter method (the parameter would be the object/value that the persistence provider retrieved from a jdbc call). One last remark: @Column without specifying any field of the annotation doesn't have an effect. [ November 26, 2008: Message edited by: Ralph Jaus ]
|
 |
Karnati Sudhakar
Ranch Hand
Joined: Aug 26, 2008
Posts: 270
|
|
|
Thanks Ralph for your time and explanation.
|
 |
 |
|
|
subject: How to write Field baed access entity
|
|
|