This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to write Field baed access entity  RSS feed

 
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 ]
 
Ranch Hand
Posts: 856
  • 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
 
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.
 
No, tomorrow we rule the world! With this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!