1

我定义了一个 ConfigurationProperty 类,它基本上是一个键值对(键是严格的正整数,值是字符串):

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable = "true")
public final class ConfigurationProperty
{
    @PrimaryKey
    @Persistent
    private Integer id;

    @Persistent
    private String value;

    public ConfigurationProperty(Integer id, String value)
    {
        this.id = id;
        this.setValue(value);
    }

    public Integer getId()
    {
        return this.id;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}

如您所见,我正在定义一个字段“id”(它是键值对的键)并将其用作类的主键。

但是,当我尝试访问一个条目时:

int searchId = 4;
ConfigurationProperty property =
        this.persistence.getObjectById(ConfigurationProperty.class, searchId);

我得到了例外:

javax.jdo.JDOFatalUserException: Received a request to find an object of type [mypackage].ConfigurationProperty identified by 4.  This is not a valid representation of a primary key for an instance of [mypackage].ConfigurationProperty.

NestedThrowables:
org.datanucleus.exceptions.NucleusFatalUserException: Received a request to find an object of type [mypackage].ConfigurationProperty identified by 4.  This is not a valid representation of a primary key for an instance of [mypackage].ConfigurationProperty.)

我几乎可以肯定,如果我为主键和对的键创建一个单独的字段,我不会遇到异常,但这会产生某种形式的冗余和可能的性能问题,因为对的键唯一的。

我也只在 Google Appengine 上得到例外。当我使用 Derby 数据库测试应用程序时,没有任何问题。

感谢您的任何建议!

4

1 回答 1

3

只需更改您的主键定义:

@PrimaryKey
@Persistent
private Integer id;

至:

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

将工作。

于 2012-06-15T13:59:06.480 回答