gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:54:19 27 4
gpt4 key购买 nike

我正在本地 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 是否使用单字段标识?没有。提示:阅读方法的 javadoc,因为它们会告诉您它们何时可用。如果您有“RawItemKey”,那么您就有“id”,因此只需使用 pm.getObjectById(id)

关于java - 无法为类 "..."创建对象标识,因为 key 的类型不受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9843488/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com