[hibernate-dev] Another @Access quandry

Steve Ebersole steve at hibernate.org
Wed Mar 26 00:13:30 EDT 2014


>From the test
org.hibernate.test.annotations.access.jpa.AccessMappingTest#testExplicitPropertyAccessAnnotationsWithHibernateStyleOverride
we have the following:


@Entity
@Access(AccessType.PROPERTY)
public class Course3 {
    private long id;
    ...

    @Id
    @GeneratedValue
    @Access(AccessType.FIELD)
    public long getId() {
        return id;
    }
    ...
}

The test asserts that this is a valid mapping.  Granted that the spec is
very unclear here, so I might be missing something.  The pertinent spec
section here states:










*<quote>When Access(PROPERTY) is applied to an entity class, mapped
superclass, or embeddableclass, mapping annotations may be placed on the
properties of that class, and the persistenceprovider runtime accesses
persistent state via the properties defined by that class. All proper-ties
that are not annotated with the Transient annotation are persistent.
WhenAccess(PROPERTY) is applied to such a class, it is possible to
selectively designate indi-vidual attributes within the class for instance
variable access. To specify a persistent instancevariable for access by the
persistence provider runtime, that instance variable must be desig-nated
Access(FIELD).</quote>*


I can see a few different ways to read that:

1) @Access can be placed on the attribute to define both where to look for
mapping annotations and the runtime access strategy for a given attribute.
 Here, we'd do:

@Entity
@Access(AccessType.PROPERTY)
public class Course3 {
    @Id
    @GeneratedValue
    @Access(AccessType.FIELD)
    private long id;
    ...

    public long getId() {
        return id;
    }
    ...
}

2) @Access can be placed on the attribute to define the runtime access
strategy for a given attribute, but the class/hierarchy AccessType controls
where to look for mapping annotations.  This would lead to:

@Entity
@Access(AccessType.PROPERTY)
public class Course3 {
    @Access(AccessType.FIELD)
    private long id;
    ...

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }
    ...
}

The test seems to illustrate that our legacy code made yet a 3rd reading of
this passage such that @Access is still considered a "mapping annotation"
even though that seems to directly contradict "To specify a persistent
instance
variable for access by the persistence provider runtime, that instance
variable must be desig-
nated Access(FIELD)."


Is there some other passage I am missing that bears on what to do here?
 How do y'all feel about that passage and its implications on this test
mapping?


More information about the hibernate-dev mailing list