liveBook · Manning

@Access annotation

As explained in Java Persistence with Spring Data and Hibernate epub

The JPA @Access annotation selects or overrides whether persistent properties are accessed through fields or property accessor methods.

@Access selects how JPA reads and writes persistent state: directly through fields or indirectly through getter and setter methods. The default strategy follows the location of @Id, but @Access can override it at the entity, class, embeddable, or individual-property level. section 6.1.2

Apply the annotation with these placement rules:

  • Use @Access(AccessType.FIELD) to access fields directly, or @Access(AccessType.PROPERTY) to use getter and setter methods. section 6.1.2
  • Annotating an entity or class selects the strategy for all its properties; mapping annotations, including @Id, may then appear on fields or getter methods. section 6.1.2
  • Annotating one property changes only runtime access for that property; other mapping annotations remain where the selected mapping style expects them. section 6.1.2
  • Do not place mapping annotations on setter methods; property access uses getter methods for mapping and getter/setter methods at runtime. section 6.1.2

Override one property

An entity using field access by default can expose one property through its getter: @Access(AccessType.PROPERTY) together with @Column(name = "ITEM_NAME") on the name field. Hibernate or Spring Data JPA using Hibernate then accesses name through its property methods while retaining the shown mapping metadata. section 6.1.2

Listing 6.1 Overriding access strategy for the name property
Path: Ch06/mapping-value-types/src/main/java/com/manning/javapersistence
 /ch06/model/Item.java
 
@Entity
public class Item {
 
    @Id                                                                     #1
    @GeneratedValue(generator = "ID_GENERATOR")                             #1
    private Long id;                                                        #1
 
    @Access(AccessType.PROPERTY)                                            #2
    @Column(name = "ITEM_NAME")                                             #2
    private String name;                                                    #2
 
    public String getName() {                                               #3
        return name;                                                        #3
    }                                                                       #3
 
    public void setName(String name) {                                      #3
        this.name =                                                         #3
                !name.startsWith("AUCTION: ") ? "AUCTION: " + name : name;  #3
    }                                                                       #3
 
}
#1 The <kbd>Item</kbd> entity defaults to field access. The <kbd>@Id</kbd> is on the field.
#2 The <kbd>@Access(AccessType.PROPERTY)</kbd> setting on the <kbd>name</kbd> field switches this particular property to be accessed at runtime by the JPA provider through getter/setter.
#3 Hibernate or Spring Data JPA using Hibernate calls <kbd>getName()</kbd> and <kbd>setName()</kbd> when loading and storing items.

Inspect the complete Item example showing a field-based entity with a property-access override for name. section 6.1.2

Embedded classes inherit their owning entity’s default or explicit access strategy. An embeddable-level or embedded-property @Access annotation can override that inheritance, determining both where mapping annotations belong and whether values are read from fields or through accessors. section 6.2.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains class, property, and embeddable overrides plus annotation placement, stopping short of broader inheritance mapping details.
Examples
Uses an Item name property override and User with an embedded Address component to demonstrate mixed and inherited access.
What it assumes
Assumes basic JPA entity and mapping annotations, while defining field access, property access, and their inheritance into embeddables.
What you can do after
Configure mixed field and property access while keeping mapping annotations in the locations required by each strategy.
How it connects
Connects field access and property access with entities, embeddable classes, mapped superclasses, and inheritance.

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.