基于包含MappedSuperclass的元模型的JPA EntityGraph不可能吗? [英] JPA EntityGraph based on meta model containing MappedSuperclass not possible?

查看:112
本文介绍了基于包含MappedSuperclass的元模型的JPA EntityGraph不可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类型安全的方法EntityGraph.addAttributeNodes(Attribute<T, ?> ... attribute)来构造我的实体图.我有一个类型层次结构,其@MappedSuperclass基本上如下所示:

I'm trying to use the type safe method EntityGraph.addAttributeNodes(Attribute<T, ?> ... attribute) for constructing my entity graph. I have a type hierarchy with a @MappedSuperclass that basically looks like this:

@MappedSuperclass
public abstract class BaseEntity
{
    @Id
    private int dbid;
}

@Entity
public class Entity extends BaseEntity
{
    private String someAttribute;
}

EclipseLink创建此元模型:

EclipseLink creates this meta model:

@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;
}

问题是我无法使用实体图API引用dbid属性:

The problem is that I cannot refer to the dbid attribute with the entity graph 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)?这是规范的缺点还是我忽视了什么?

For this to work, wouldn't the method signature need to look like this: EntityGraph.addAttributeNodes(Attribute<? super T, ?> ... attribute)? Is this a shortcoming of the specification or am I overlooking something?

在我看来,这是与

It seems to me that this is a problem related to the one described here. As the author of that question points out, the Criteria API get method for singular attributes does indeed use ? super X to define the type parameter.

但是,即使我添加了someAttribute节点,仍然会有一些难看的警告,我认为最多只能将其抑制:

But even when I add the someAttribute node, there's still this somewhat ugly warning, which I presume can only be suppressed at best:

graph.addAttributeNodes( Entity_.someAttribute ); // generates warning: "Type safety: A generic array of Attribute<Entity,?> is created for a varargs parameter"

推荐答案

我同意.

很明显,如果您将代码更改为

Clearly if you changed your code to

EntityGraph<BaseEntity> graph = em.createEntityGraph(BaseEntity.class);
graph.addAttributeNodes(BaseEntity_.dbid );

然后它将进行编译. 问题确实确实存在于spec/API中,在该规范中,已将EntityGraph的通用类型应用于addAttributeNodes参数(因此不允许超类字段).是的,它确实说"T"是 root 实体的类型,但这并不意味着他们期望人们总是使用MappedSuperclass?

then it would compile. The problem does indeed seem to be in the spec/API where it has EntityGraph's generic type being applied to the addAttributeNodes argument (hence not allowing superclass fields). Yes, it does say that "T" is the type of the root entity, but that can't mean that they expect people to always use the MappedSuperclass?

我还将确认通过对属性泛型类型使用"? super T"来修复该问题(采用javax.persistence jar源并修改/运行).

I'd also confirm that by using "? super T" for the Attribute generic type fixes it (taking a javax.persistence jar source and modifying/rerunning).

我将其作为JPA上的问题提出,而不是我建议持有一口气更新

I raised it as an issue on JPA, not that I'd recommend holding breath for an update

这篇关于基于包含MappedSuperclass的元模型的JPA EntityGraph不可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆