无法为类“...”创建对象标识,因为密钥属于不受支持的类型

时间:2012-03-23 17:08:17

标签: java jdo datanucleus

我在本地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;

    }

}

Key类是:

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)

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

1 个答案:

答案 0 :(得分:2)

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