DataNucleus可以坚持使用泛型参数化的抽象基类吗? [英] Can DataNucleus persist an abstract base class parameterized with Generics?

查看:71
本文介绍了DataNucleus可以坚持使用泛型参数化的抽象基类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用DataNucleus,我很高兴使用抽象基类为子类提供长ID(请注意原始类型)。



在改编来自JPA的示例时,我意识到参数化基类。目的是支持不同的ID类型,比如String。

  @PersistenceCapable 
@Inheritance(strategy = InheritanceStrategy。 SUBCLASS_TABLE)
@Version(strategy = VersionStrategy.VERSION_NUMBER,column =jdo_version)
public abstract class VersionedIdEntity< P>
实现了Serializable {

static final long serialVersionUID = 1L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
private P id;

public P getId(){
return id;






$ p泛型让我使用包装的Java类型'Long' ,所以子类声明如下:

  public class Account extends VersionedIdEntity< Long> 

我切换到新的基类导致了一个错误,指出'java.lang.Object '被发现。
$ b


org.datanucleus.exceptions.NucleusUserException:字段VersionedIdEntity.id被声明为引用类型(interface / Object ),但没有找到java.lang.Object的实现类!


错误与类型擦除有关吗?类型可在DataNucleus中持久化,例如:

 < ;?扩展PersistableType> 

虽然我在其他地方找不到帮助,但问题之前可能已经提出。在这种情况下,一个简短的指针将是非常值得赞赏的。

解决方案

你的意思是JDO定义了一个实现应该支持这个吗?答:不。
JDO定义了有效的PK类型,什么是P?就类而言,它是Object(即(java.lang。)Object,因为它没有实际的类型)。 JPA也没有为此定义它。


Using DataNucleus, I have been happy using an abstract base class to provide a 'long' ID to the sub-classes (please note the primitive type).

When adapting an example from JPA I got the idea to parameterize the base class. The purpose was to support different ID types, such as String.

@PersistenceCapable
@Inheritance(strategy=InheritanceStrategy.SUBCLASS_TABLE)
@Version(strategy=VersionStrategy.VERSION_NUMBER, column="jdo_version")
public abstract class VersionedIdEntity<P>
    implements Serializable {

  static final long serialVersionUID = 1L;

  @PrimaryKey
  @Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
  private P id;

  public P getId() {
    return id;
  }
}

Generics made me use the wrapped Java type 'Long', so sub-classes were declared like this:

public class Account extends VersionedIdEntity<Long>

My switch to the new base class caused an error stating that no implementation for 'java.lang.Object' was found.

org.datanucleus.exceptions.NucleusUserException: Field "VersionedIdEntity.id" is declared as a reference type (interface/Object) but no implementation classes of "java.lang.Object" have been found!

Is the error related to type erasure?

Additional questions would be (1) how JPA deals with it and (2) whether I can restrict the type P to types persistable in DataNucleus, such as:

<? extends PersistableType>

While I could not find help elsewhere, the question could have been asked before. In that event, a brief pointer would be much appreciated.

解决方案

You mean does JDO define that an implementation should support this? Answer : no. JDO defines the valid PK types, and what is a "P" ? it is Object as far as a class is concerned (i.e (java.lang.)Object, since it doesn't have the real type there). Neither does JPA define it for that matter.

这篇关于DataNucleus可以坚持使用泛型参数化的抽象基类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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