Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Hibernate warning? '@Access(AccessType.PROPERTY) on a field has no effect'

Tags:

People also ask

What is access annotation in hibernate?

@Access in hibernate is used to force access hierarchy of an entity methods and fields. In class when we use @Id or @Embedded on field, then field is persisted and used to access data by setter and getter. If @Id or @Embedded is used on property then access hierarchy will be property.

Which are valid access strategy for a class in hibernate?

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access.

What is @access annotation in JPA?

JPA provides @Access annotation for overriding the default behavior, by using AccessType. FIELD and AccessType. PROPERTY. If you set @Access on the class/entity level, JPA accesses all properties of the class according to the selected strategy irrespective of place of Id/EmeddedId annotation.


I have following entity class User:

public class User implements Serializable {

    @Column(length = 10, name = "user_type")
    @Access(AccessType.PROPERTY)
    private String userTypeS;

    @Transient
    private UserType userType;
    ...
    public void setUserType(UserType userType) {
        this.userType = userType;
        this.userTypeS = this.userType.getType();
    }

    protected void setUserTypeS(String userTypeS) {
        this.userTypeS = userTypeS;
        userType = UserType.toUserType(userTypeS);
    }

UserType is an enum. This issue was that I could not simply use the @Enumerated annotation to map UserType since the presentation of the enums in the code is different from that in the, e.g.:

public enum UserType {
CUSTOMER_NON_PRO("custnop") ...

When workign with the entity I wanted to be able to set the enum as userType and not the String representation of it. For this I created a public setter that sets the enum (userType) and translates it to the String representation of it (which is mapped with hibernate). Similarly there is a protected setUserTypeS which must be called by hibernate and will map the String userType to the enum UserType.

For this of course hibernate must use the setters to populate the entity. In our project it is preferred to set the annotations on the properties themselves i.o. the getters/setters. Hence hibernate will set the property values directly using introspection (and will thereby bypass the setters). For userTypeS I indicated that the access type is PROPERTY (i.o. FIELD) and because of that hibernate will call setUSerTypeS.

All of this works smoothly. The 'problem' is that in our logs we see following warning appear:

org.hibernate.cfg.AnnotationBinder - Placing @Access(AccessType.PROPERTY) on a field does not have any effect.

This warning does not appear to be correct. If I would remove @Access(AccessType.PROPERTY) from the field userTypeS then hibernate would not call the setter and hence the enum userType would not be set. So, placing @Access(AccessType.PROPERTY) does have affect.

Is this warning message invalid or outdated or am I misunderstanding something anyway?

thanks,
Stijn