I have some questions regarding the @Access annotation of javax.persistence.
some background: I updated my hibernate version from 5.0.3.Final to 5.4.24.Final.
for some reason, after the upgrade, the app did not load because I had a problem with some of my entity classes (not all).
The common factor between those entities was that all of them were Triple inheritance.
example:
@MappedSuperclass
public abstract class BaseEntry {
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "ID", insertable = false, updatable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@MappedSuperclass
public class ManagedIncidentEntry extends BaseEntry {
private String type;
@Column(name = "TYPE")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
@Entity
@Table(name = "MANAGED_INCIDENTS")
public class ManagedIncidentRawInfoEntry extends ManagedIncidentEntry {
private String additionalInfo;
@Column(name = "ADDITIONAL_INFO")
public String getAdditionalInfo() {
return additionalInfo;
}
public void setAdditionalInfo(String additionalInfo) {
this.additionalInfo = additionalInfo;
}
}
after reviewing the user guide https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html
I saw some example in the UG with @Access annotation and I play with the annotation , each time used a different value or placed it in different parts in the code and eventually the problem was fixed.
this in the middle class after the fix.
@MappedSuperclass
@Access( AccessType.FIELD )
public class ManagedIncidentEntry extends BaseEntry {
private String type;
@Access( AccessType.PROPERTY )
@Column(name = "TYPE")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Now I want to learn as much as I can about this annotation.
why did I need to use it in the first place? meaning why the app worked without it on hibernate version 5.0.3.Final?
what is the purpose of this annotation? I found this answer What is the purpose of AccessType.FIELD, AccessType.PROPERTY and @Access but I still don't understand the purpose. the db needs it to know from where to pull the value? from the field or from the getter?
in the answer above they talked about the different between AccessType.FIELD and AccessType.PROPERTY
If you put them on the field - it will be AccessType.FIELD, if you put them on the getters - it will be AccessType.PROPERTY.
but when would I want to put it above a getter and when would I want to put it above a field?
when would I want to put the the annotation above a specific field/getter and when would I want to put the annotation above the whole class
are they specific kinds of java members that doesn't support the annotation? for example, when I played with the annotation, for some reason it didn't like it when I put the @Access( AccessType.FIELD ) above List
why I needed both @Access( AccessType.FIELD ) above the class name and @Access( AccessType.PROPERTY ) above the getter? without the @Access( AccessType.FIELD ) didn't work (tomcat 10) and without the @Access( AccessType.PROPERTY ) the junit didn't work so I needed both
additional info - im using java 8 spring 5.3.25