jakartaee / jakartaee/persistence
define behavior when using type variables
- Dominant language
- Java
- Stars
- 268
- Forks
- 78
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 13
Description
Currently when having a base class for entities BaseEntity with a type variable it is not defined what the metamodel should report as type for attributes that use a type variable. Let's consider the following example.
```
@MappedSuperclass
public class BaseEntity {
@EmbeddedId
private T id;
}
@Embeddable
public class MyEntityId implements Serializable {
private String someValue;
}
@Entity
public class MyEntity extends BaseEntity {
}
```
When asking the metamodel for the id type of MyEntity through EntityType.getIdType() it is undefined what it should report. Some JPA implementations return the type bound Serializable, others return the actual type MyEntityId and some even return something completely different String.
Analogous to that is that the return of Attribute.getJavaType() which is also undefined for the attribute **id**.
I propose to add another method to `javax.persistence.metamodel.Attribute` that allows to resolve the attribute type based on a container class like
```
public interface Attribute {
default Class getJavaType(Class containerClass) {
return getJavaType();
}
// other methods }
```
It should be clarified that getJavaType() must return the type bound i.e. Serializable in this case. The use of the concrete subtype as container class like idAttribute.getJavaType(MyEntity.class) should resolve to the correct type MyEntityId.
#### Affected Versions
[2.1.1]
Contributor guide
Research direction
Start by reading the metamodel API entry points Attribute.getJavaType() and EntityType.getIdType(), then review the generic BaseEntity example and the differing implementation behaviors described. Done means reaching an agreed specification for type-variable resolution and any corresponding API change; the issue does not identify tests or implementation files.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100