[英]How to store Key-Value pairs using JDO (datanucleus & appengine)
我已經定義了一個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數據庫測試應用程序時,沒有任何問題。
謝謝你的任何建議!
只需更改主鍵定義:
@PrimaryKey
@Persistent
private Integer id;
至:
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
將工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.