无法为类"..."创建对象标识,因为键的类型不受支持



我在本地H2数据库上使用DataNucleus的JDO实现。我想检查数据库中是否存在一个知道其密钥的对象实例。

对象定义如下:

@PersistenceCapable(objectIdClass=RawItemKey.class)
@Index(name="CONTAIN_IDX", members={"prefix", "language", "value"})
public class RawContainItem {
    @PrimaryKey
    @Column(length=40)
    String prefix = "";
    @PrimaryKey
    @Column(length=2)
    String language = "";
    @PrimaryKey
    @Column(length=Integer.MAX_VALUE)
    String value = "";    
    public RawContainItem(String prefix, String language, String value) {
        this.prefix = prefix;
        this.language = language;
        this.value = value;
    }
}

关键类是:

public class RawItemKey implements Serializable {
    public String prefix = "";
    public String language = "";
    public String value = "";
    public static final char SEPARATOR = 'u2407';
    public RawItemKey() {
    }
    public RawItemKey(String retr) {
        int beg = retr.indexOf(SEPARATOR);
        int end = retr.lastIndexOf(SEPARATOR);
        if ( beg >= 0 ) {
            this.prefix = retr.substring(0, beg);
        }
        if ( end > 0 ) {
            this.value = retr.substring(end+1, retr.length());
        }
        if ( ( beg >= 0 ) && ( end > 0 ) ) {
            this.language = retr.substring(beg+1, end);
        }
    }
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (this.prefix != null ? this.prefix.hashCode() : 0);
        hash = 89 * hash + (this.language != null ? this.language.hashCode() : 0);
        hash = 89 * hash + (this.value != null ? this.value.hashCode() : 0);
        return hash;
    }
    @Override
    public boolean equals(Object obj) {
        if (obj == this) { return true; }
        if (obj == null) { return false; }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final RawItemKey other = (RawItemKey) obj;
        if ((this.prefix == null) ? (other.prefix != null) : !this.prefix.equals(other.prefix)) {
            return false;
        }
        if ((this.language == null) ? (other.language != null) : !this.language.equals(other.language)) {
            return false;
        }
        if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) {
            return false;
        }
        return true;
    }
    @Override
    public String toString () {
        return this.prefix + SEPARATOR + this.language + SEPARATOR + this.value;
    }    
}

我使用的代码:

RawItemKey tmp = new RawItemKey();
tmp.prefix = d.prefix;
tmp.language = "EN";
tmp.value = retr;
RawContainItem rretr = PM.getObjectById(RawContainItem.class, tmp);
if ( rretr == null ) {
    PM.makePersistent(new RawContainItem(d.prefix, "EN", retr));
}                                     

我得到的错误:

org.datanucleus.exceptions.NucleusUserException:
 Unable to create Object Identity for class "net.dwst.findword.DataNucleus.RawBeginItem"
 since key is of an unsupported type (net.dwst.findword.DataNucleus.RawItemKey)
    at org.datanucleus.ObjectManagerImpl.newObjectId(ObjectManagerImpl.java:3362)
    at org.datanucleus.api.jdo.JDOPersistenceManager.newObjectIdInstance(JDOPersistenceManager.java:1627)
    at org.datanucleus.api.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1749)
    at net.dwst.findword.EN.FeedDatabaseEN1.feed(FeedDatabaseEN1.java:92)
    at net.dwst.findword.EN.FeedDatabaseEN1.main(FeedDatabaseEN1.java:32)

导致此错误的原因是什么?如何检查对象是否存在于数据库中?

RawContainItem是否使用单个字段标识?没有。提示:阅读方法的javadocs,因为它们会告诉您它们何时可用。如果你有一个"RawItemKey",那么你就有了"id",所以只需使用pm.getObjectById(id)

最新更新