简体   繁体   中英

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

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, eg:

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 io 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 (io 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:

@Access(AccessType.PROPERTY)
@Column(length = 10, name = "user_type")
public String getUserType() {
    return this.userType.getType();
}

public void setUserType(UserType userType) {
    this.userType = userType;
}

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

private String userTypeS; is not needed at all.

I have no solution for you but according to JPA 2.0 spec (page 26 footnote 8):

It is not permitted to specify a field as Access(PROPERTY) or a property as Access(FIELD)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM