jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
187 stars 55 forks source link

defaulted attribute access type for a hierarchy when root is annotated @Access #558

Open gavinking opened 6 months ago

gavinking commented 6 months ago

If the root of an entity class hierarchy is annotated @Access, then it seems to me that the spec is very unclear on how the default access type for the class hierarchy is determined. [The effect of @Access is not inherited, and I don't think it should be.]

So what is the expected behavior if a subclass does not explicitly specify @Access?

Either:

  1. it's an error: all classes in an entity hierarchy with a root annotated @Access must also be annotated @Access, or
  2. we consider all subclasses of the root class in determining the default access type.

I imagine that 2 is what was intended, but it's unclear. And, since the @Id attribute is declared by the root entity, the subclasses in principle might not have any mapping annotations allowing the default access type to be inferred. Consider:

@Entity @Access(FIELD)
class Root {
    @Id String id;
}

@Entity
class Child extends Root {
    String name;
    String getName();
    void setName(String name);
}

what is the access type of Child? I don't think it has one.

We need to say something more explicit here.

sebersole commented 6 months ago

First, I'm not going to argue that chapter 2.3 Access Type is confusing. I personally find it one of the more unclear and confusing chapters in the specification.

we consider all subclasses of the root class in determining the default access type.

The spec is pretty clear in my opinion that subclasses should not be considered for determination of "default access type" of the hierarchy. I think that actually makes sense.

The default access type of an entity hierarchy is determined by the placement of mapping annotations on the
attributes of the entity classes and mapped superclasses of the entity hierarchy that do not explicitly
specify an access type.

In your example, assuming we extended Child as Grandchild with more non-annotated attributes, scanning the subclasses still does not give a positive determination about the "default access type", depending on the rest of my comment.

To me, the confusion comes from whether @Id on Root meets that criteria or not. It feels wrong to me that it should given @Access on the class - the class is already defining an "explicit access type" (section 2.3.2 rather than 2.3.1) which would seem to imply @Access on the field.

The effect of @Access is not inherited, and I don't think it should be.

I don't necessarily agree with that. I agree that this is what the specification intends, and in fact it says that much -

This explicit access type specification does not affect the access type of other entity classes or mapped superclasses in the entity hierarchy.

My disagreement here is that I can easily argue that it makes more sense due to localness. To me, I could argue this either way which in-and-of itself demonstrates the lack of clarity.

Personally, I think determination of the "default access type" for the hierarchy ought to consider explicit @Access on the root or its mapped-superclasses if present. I think of "default access type" as "implicit access type for the hierarchy" which is another wording the specification uses sometimes to decribe this. Using that terminology, I think it is easier to see why that @Access should determine the "default access type".

gavinking commented 4 months ago

@lukasj we should probably get on zoom sometime just to talk this one through.

Perhaps there's something sensible we could say to clarify this issue. I'm not very clear on what it would be, but let's see if we can work it out.