Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write Field baed access entity

 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

package Database;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity()
@Table(name="EMPLOYEETABLE")
public class Employee implements Serializable {

@Embedded
protected Address address;

public enum Employeetype {contract, permanent};

@Enumerated(EnumType.STRING)
protected Employeetype emptype;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

protected String EmployeeName;

@Temporal(TemporalType.TIMESTAMP)
protected Date creationdate;

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 ]
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ralph for your time and explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic