我正在尝试使用类型安全的方法EntityGraph.addAttributeNodes(Attribute<T, ?> ... attribute)
来构建我的实体图。我有一个类型层次结构,@MappedSuperclass
它基本上看起来像这样:
@MappedSuperclass
public abstract class BaseEntity
{
@Id
private int dbid;
}
@Entity
public class Entity extends BaseEntity
{
private String someAttribute;
}
EclipseLink 创建了这个元模型:
@Generated(value="EclipseLink-2.5.2.v20140319-rNA", date="2015-08-07T10:46:31")
@StaticMetamodel(BaseEntity.class)
public abstract class BaseEntity_ {
public static volatile SingularAttribute<BaseEntity, Integer> dbid;
}
@Generated(value="EclipseLink-2.5.2.v20140319-rNA", date="2015-08-07T10:46:31")
@StaticMetamodel(Entity.class)
public class Entity_ extends BaseEntity_ {
public static volatile SingularAttribute<Entity, String> someAttribute;
}
问题是我无法dbid
使用实体图 API 引用属性:
EntityGraph<Entity> graph = em.createEntityGraph( Entity.class );
graph.addAttributeNodes( Entity_.dbid ); // does not compile: "The method addAttributeNodes(String...) in the type EntityGraph<Entity> is not applicable for the arguments (SingularAttribute<BaseEntity,Integer>)"
为此,方法签名是否不需要如下所示:EntityGraph.addAttributeNodes(Attribute<? super T, ?> ... attribute)
?这是规范的缺点还是我忽略了什么?
在我看来,这是与此处描述的问题相关的问题。正如该问题的作者所指出的,get
奇异属性的 Criteria API 方法确实用于? super X
定义类型参数。
但是即使我添加了someAttribute
节点,仍然有这个有点难看的警告,我认为它充其量只能被抑制:
graph.addAttributeNodes( Entity_.someAttribute ); // generates warning: "Type safety: A generic array of Attribute<Entity,?> is created for a varargs parameter"