2010-07-18 51 views
0

我試圖使用DataNucleus 2.1.1插入一個Employee對象到數據庫中,但是在執行「insert」準備語句之前沒有設置外鍵位置參數。我在做什麼,防止參數被設置?我是否留下了一些東西?閱讀工作正常。準備好的語句參數沒有在DataNucleus中設置

DEBUG [DataNucleus.Datastore.Native] - INSERT INTO MYSCHEMA.EMPLOYEE ("NAME",POSITION_ID,ID) VALUES (<'John Doe'>,<UNPRINTABLE>,<100>) 
WARN [DataNucleus.Datastore.Persist] - Insert of object "[email protected] 7895d8" using statement "INSERT INTO MYSCHEMA.EMPLOYEE ("NAME",POSITION_ID,ID) VALUES (?,?,?)" failed : Invalid operation: parameter 2 not set or registered 

PersistenceManager pm = // obtain PersistenceManager 
Position position = // obtain Position using pm (I can post this code upon request) 
Employee employee =new Employee(100, "John Doe", position); 
pm.makePersistent(employee); 
pm.close(); 


<jdo> 
<package name="com.example.staff"> 
    <class name="Position" identity-type="application" schema="MYSCHEMA" table="Position"> 
     <inheritance strategy="new-table"/> 
     <field name="id" primary-key="true" persistence-modifier="persistent" default-fetch-group="true"> 
      <column name="ID" jdbc-type="integer"/> 
     </field> 
     <field name="title" persistence-modifier="persistent" default-fetch-group="true"> 
      <column name="TITLE" jdbc-type="varchar"/> 
     </field> 
    </class> 
</package> 
</jdo> 

<jdo> 
<package name="com.example.staff"> 
    <class name="Employee" identity-type="application" schema="MYSCHEMA" table="EMPLOYEE"> 
     <inheritance strategy="new-table"/> 
     <field name="id" primary-key="true" persistence-modifier="persistent" default-fetch-group="true"> 
      <column name="ID" jdbc-type="integer"/> 
     </field> 
     <field name="name" persistence-modifier="persistent" default-fetch-group="true"> 
      <column name="NAME" jdbc-type="varchar"/> 
     </field> 
     <field name="position" persistence-modifier="persistent" default-fetch-group="true"> 
      <column name="POSITION_ID" jdbc-type="integer" /> 
     </field> 
    </class> 
</package> 
</jdo> 

@PersistenceCapable 
public class Employee extends Object { 
    private Integer id; 
    private String name; 
    private Position position; 

    public Employee(Integer id, String name, Position position) { 
     this.id =id; 
     this.name =name; 
     this.position =position; 
     } 
} 

@PersistenceCapable 
public class Position extends Object { 
    private Integer id; 
    private String title; 
} 

回答

0

我沒有這樣的問題這樣做。在我的情況下,我使用pm.getObjectById(id)獲取Position對象。

+0

是的,我應該在測試用例中使用getObjectById()。任何想法可能會導致這種事情?我從來沒有想到,我可以做或不做任何事情會阻止持久層設置參數。 – user371320 2010-07-20 18:30:15

+0

你不說*你怎麼得到你的案件中的對象。我使用getObjectById,因爲這是唯一明顯的方法。也許你用別的東西?不知道還有什麼沒有人能說的 – DataNucleus 2010-09-21 07:21:56