views:

130

answers:

1

I'm trying to insert an Employee object into the database using DataNucleus 2.1.1, but the foreign-key Position parameter is not being set before the "insert" prepared statement is executed. What am I doing that prevents the parameter from being set? Am I leaving something out? Reading works fine.

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;
}
A: 

I have no such problems doing that. In my case I'm getting the Position object using pm.getObjectById(id).

DataNucleus
Yes, I should have used getObjectById() in the test case. Any ideas on what might cause this sort of thing? It never occurred to me that anything I could do or omit would prevent the persistence layer from setting a parameter.
You don't say *how* you get the object in your case. I used getObjectById since that is the only obvious method. Maybe you used something else ? and without knowing what else there's not much anyone can say
DataNucleus