0

我正在尝试使用 DataNucleus 2.1.1 将 Employee 对象插入数据库,但在执行“插入”准备语句之前未设置外键位置参数。我在做什么阻止设置参数?我是不是遗漏了什么?阅读效果很好。

DEBUG [DataNucleus.Datastore.Native] - INSERT INTO MYSCHEMA.EMPLOYEE ("NAME",POSITION_ID,ID) VALUES (<'John Doe'>,<UNPRINTABLE>,<100>)
WARN  [DataNucleus.Datastore.Persist] - Insert of object "com.example.staff.Employee@50 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;
}
4

1 回答 1

0

我这样做没有这样的问题。就我而言,我使用 pm.getObjectById(id) 获取 Position 对象。

于 2010-07-20T07:33:21.607 回答