我有一个现有的Oracle DB,其中有2个表对应于下面的类。 在DB中,已存在FK b / w AllCodes和AllCodesHistory,以便AllCodesHistory中的ALLCODES列包含来自AllCodes的主键。
但是,当我执行下面的代码时,
[code]
PersistenceManager pm = JdoPersistenceManager.getPersistenceManagerFacotry().getPersistenceManager();
Object[] o = null;
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
System.out.println("Quering....");
int objectId = 1234;
q = pm.newQuery("SELECT FROM AllCodesHistoryJdoImpl where allcodes == " + objectId);
obj = (List)q.execute();
System.out.println(obj);
//System.out.println("Detaching....done");
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
[/code]
我得到以下异常:
[code]
Exception in thread "main" javax.jdo.JDOUserException: Variable allcodes is unbound and cannot be determined
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:549)
at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:230)
at test.Test.main(Test.java:38)
[/code]
为什么我收到此错误的任何想法?最后给出了类定义和映射。
在使用Data nucleus中的schemaTool(并将输出重定向到文件)时,我看到DN不承认FK已经创建并映射到ALLCODES的PK,而是尝试创建自己的专栏和FK。
ALTER TABLE ALLCODESHISTORY ADD ALLCODES_OBJECTID_OID NUMBER (38) NULL;
ALTER TABLE ALLCODESHISTORY ADD CONSTRAINT ALLCODESHISTORY_FK1 FOREIGN KEY (ALLCODES_OBJECTID_OID) REFERENCES ALLCODES (OBJECTID) INITIALLY DEFERRED ;
这可以避免吗? 在此先感谢。
[code]
public class AllCodesJdoImpl {
public int objectId;
public String code;
public AllCodesJdoImpl() {
}
public void setMasterCode(String value) throws appException {
this.masterCode = value;
}
public String getCode() throws appException {
return this.masterCode;
}
public int getObjectId() throws appException {
// TODO Auto-generated method stub
return objectId;
}
}
public class AllCodesHistoryJdoImpl implements AllCodesHistory {
//
public String oldValue;
public int objectId;
public AllCodesJdoImpl allCodes;
public AllCodesHistoryJdoImpl() {
}
public String getOldValue() throws appException {
return null;
}
public void setOldValue(String value) throws appException {
return oldValue;
}
public AllCodes getAllCodes() throws appException {
return allCodes;
}
public void setAllCodes(AllCodes value) throws appException {
allCodes = (AllCodesJdoImpl)value;
}
public int getObjectId() throws appException {
// TODO Auto-generated method stub
return objectId;
}
}
The corresponding mapping is as below:
<class name="AllCodesJdoImpl" table="ALLCODES">
<field name="code" >
<column name="CODE" length="35" jdbc-type="VARCHAR" />
</field>
<field name="objectId" primary-key="true">
<column name="OBJECTID" length="38" jdbc-type="INTEGER" />
</field>
</class>
<class name="AllCodesHistoryJdoImpl" table="ALLCODESHISTORY">
<field name="oldValue" >
<column name="OLDVALUE" length="255" jdbc-type="VARCHAR" />
</field>
<field name="allCodes" >
<element column="ALLCODES"/>
</field>
<field name="objectId" primary-key="true">
<column name="OBJECTID" length="38" jdbc-type="INTEGER" />
</field>
</class>
[/code]
答案 0 :(得分:0)
你拼错了字段名称。 Java区分大小写