What is the purpose of AccessType.FIELD, AccessType.PROPERTY and @Access

64,553

By default the access type is defined by the place where you put your identifier annotation (@Id). If you put it on the field - it will be AccessType.FIELD, if you put it on the getter - it will be AccessType.PROPERTY.

Sometimes you might want to annotate not fields but properties (e.g. because you want to have some arbitrary logic in the getter or because you prefer it that way.) In such situation you must define a getter and annotate it as AccessType.PROPERTY.

As far as I remember, if you specify either AccessType.FIELD or AccessType.PROPERTY on any of the entity fields / methods you must specify the default behaviour for the whole class. And that's why you need to have AccessType.FIELD on the class level (despite that AccessType.FIELD is the default value.)

Now, if you wouldn't have @Transient on the phnnumber field, the JPA would provide you with a 3 columns table:

  • id,
  • phnnumber,
  • getphnnumber.

That's because it would use AccessType.FIELD for all of the entity fields (id and phnnumber) and, at the same time, it'd use AccessType.PROPERTY for your getter (getPhnnumber()).
You'll end with phone number mapped twice in the database.

Therefore, the @Transient annotation is required - it means that the entity won't store the value of the field in the underlying storage but the value returned by your getter.

Share:
64,553
Ashish Bansal
Author by

Ashish Bansal

Updated on July 08, 2022

Comments

  • Ashish Bansal
    Ashish Bansal almost 2 years

    I just want to know what is the difference between all these annotations. Why are we using these... means they have no effect especially field level and property level.

    And what is the purpose of using mixed level annotation like:

    @Entity
    @Access(AccessType.FIELD)
    class Employee {
        // why their is a field level access 
        private int id;
    
        // whats the purpose of transient here
        @Transient                               
        private String phnnumber;
    
        // why its a property level access
        @Access(AccessType.property)             
        public String getPhnnumber() {
            return "1234556";
        }
    
    }
    

    what exactly this class says?

  • JB Nizet
    JB Nizet over 11 years
    The default is not FIELD. The access type is FIELD if you place mapping annotations on fields, and it's PROPERTY if you place mapping annotations on getters. And all the entity hierarchy must be coherent in the mapping annotation placement: always on fields, or always on getters, but not mixed.
  • Piotr Nowicki
    Piotr Nowicki over 11 years
    You're right - the default is dependent on where the @Id annotation is located. About the mixing - you're referring only to the id annotation or to the fact that you can't mix the property / field access at all in the entity hierarchy?
  • JB Nizet
    JB Nizet over 11 years
    If you don't explicitely specify access type, the JPA spec says that all the mapping annotations in the hierarchy must be placed either on fields, or on getters. What happens if you don't respect the rule is not specified though. Hibernate looks up where the Id annotation is, and if it's on field it ignores all the annotations on getters (and vice-versa), but this is Hibernate-specific. The behavior in such a case is undefined (that's what the spec says).
  • Piotr Nowicki
    Piotr Nowicki over 11 years
    I could swear I've read about @Id in "Pro JPA 2.0: Mastering the Java Persistence" but I just checked the spec and it really doesn't talk about the Id at all; just about mapping annotations you've mentioned. Thanks for the clarification JB!
  • Piotr Nowicki
    Piotr Nowicki over 11 years
    Ok, I just found out that in the book it was @Id because it was the only mapping annotation in the example...
  • sofs1
    sofs1 about 6 years
    "As far as I remember, if you specify either AccessType.FIELD or AccessType.PROPERTY on any of the entity fields / methods you must specify the default behaviour for the whole class." - Why should one specify on class level?