In JPA the state of an entity can be accessed either by FIELD or PROPERTY.Those two access modes define the way that JPA will persist and initialize the entity with data from the database and in this tutorial we will see what access by FIELD and PROPERTY are and what @Access annotation is.

Access by Field

In its simplest form,field access can be achieved by annotating the fields of the entity class.For example:

package com.javaonly.model;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Tutorial {

    @Id
    private int id;
    private String title;
    private String summary;
    private String body;
    private int negativeVotes;
    private int possitiveVotes;
    private Category category;
    private Date dateAdded;

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getBody() {
        return this.body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public int getNegativeVotes() {
        return this.negativeVotes;
    }

    public void setNegativeVotes(int negativeVotes) {
        this.negativeVotes = negativeVotes;
    }

    public int getPossitiveVotes() {
        return this.possitiveVotes;
    }

    public void setPossitiveVotes(int possitiveVotes) {
        this.possitiveVotes = possitiveVotes;
    }

    public Category getCategory() {
        return this.category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Date getDateAdded() {
        return this.dateAdded;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }
}

In the above class we've annotated the id field with @Id annotation and in this way we've instructed EntityManager to use the id field instead of the id property .Note that getter and setter method still exist in this POJO for the id property but EntityManager will access the Id property without using the getter or setter method.

Access by Property

package com.javaonly.model;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Tutorial {
    private int id;
    private String title;
    private String summary;
    private String body;
    private int negativeVotes;
    private int possitiveVotes;
    private Category category;
    private Date dateAdded;

    @Id
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getBody() {
        return this.body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public int getNegativeVotes() {
        return this.negativeVotes;
    }

    public void setNegativeVotes(int negativeVotes) {
        this.negativeVotes = negativeVotes;
    }

    public int getPossitiveVotes() {
        return this.possitiveVotes;
    }

    public void setPossitiveVotes(int possitiveVotes) {
        this.possitiveVotes = possitiveVotes;
    }

    public Category getCategory() {
        return this.category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Date getDateAdded() {
        return this.dateAdded;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }
}

As you can see we have annotated the getId() method and in this way the EntityManager will use the getter/setter methods to persist the entity.

The @Access annotation

The other way to configure the acces mode of the EntityManager is with @Access annotation.With @Access annotation we can configure the access mode for the entity class as a whole or explicitly for each property. For example

package com.javaonly.model;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@Access(AccessType.FIELD)
public class Tutorial {
//properties and methods go here
}

In the above example the EntityManager will have field access for all the properties of this entity class.Suppose now that although we want our entity class to be accessed by its fields generally, we want the dateAdded property to be accessed through its getter and setter method.This can be achieved by annotating the getDayAdded() method with @Access(AccessType.Property) annotation and @Transient annotation the dateAdded filed.For example:

package com.javaonly.model;

import java.util.Date;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
@Access(AccessType.FIELD)
public class Tutorial {

    @Id
    private int id;
    private String title;
    private String summary;
    private String body;
    private int negativeVotes;
    private int possitiveVotes;
    private Category category;
    @Transient
    private Date dateAdded;

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getBody() {
        return this.body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public int getNegativeVotes() {
        return this.negativeVotes;
    }

    public void setNegativeVotes(int negativeVotes) {
        this.negativeVotes = negativeVotes;
    }

    public int getPossitiveVotes() {
        return this.possitiveVotes;
    }

    public void setPossitiveVotes(int possitiveVotes) {
        this.possitiveVotes = possitiveVotes;
    }

    public Category getCategory() {
        return this.category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
    
    @Access(AccessType.PROPERTY)
    public Date getDateAdded() {
        return this.dateAdded;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }
}

Note in the above example that marking dateAddeed field with @Transient annotation is necessary because otherwise dateAdded will be persisted twice.