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
@Access(AccessType.PROPERTY) is meant to be placed on a getter, like:
private String userTypeS;is not needed at all.