2013-12-17 76 views
1

我有一個現有的Oracle數據庫,其中有兩個表對應於下面的類。 在數據庫中,已經存在FK b/w AllCodes和AllCodesHistory,因此AllCodesHistory中的ALLCODES列包含AllCodes中的主鍵。用於1-1關係和JDO映射的現有Oracle數據庫

然而,當我執行下面的代碼,

[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核中的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

您拼錯了字段名稱。 Java區分大小寫

+0

謝謝尼爾。我忘了在這裏更新。你是對的,它應該是「allCodes」。還需要將「」的映射更改爲「」。 – Avita