Re: strange DataContext? behaviour

From: Craig Miskell (cmiskel..lbatross.co.nz)
Date: Thu Dec 12 2002 - 14:41:35 EST

  • Next message: martin ruff: "Re: strange DataContext? behaviour"

    I'll assume that where you wrote:
    newexpensesentry.ToExpensetype((...)

    you meant
    newexpensesentry.setToExpensetype((...)
    -----------------^^^

    (If not, then that would be *very* interesting and maybe indicative of a
    problem).

    As to what the real problem may be:
    Assuming that you are setting the toProjects in the same way as below
    (obtaining it from theDataContext.registeredObject, by primary key),
    then the object returned will be "hollow". The object (Project) will
    not be fetched from the db until it's properties are read. So, the big
    question is how are you reading it's properties? If you are using
    readPropertyDirectly, then you are seeing expected behaviour....
    readPropertyDirectly will not automatically fetch a HOLLOW object. You
    have to use either readProperty (defined on CayenneDataObject rather
    than DataObject), or the standard accessor methods (getTitle() etc.)

    If you are using the standard accessor methods, as generated by Cayenne,
    then something else strange is occuring. More code would help, if
    you're permitted to send it (e-mail me privately if public distribution
    is an issue).

    Hope this (or something else) helps,
    Craig Miskell

    On Fri, 2002-12-13 at 07:03, martin ruff wrote:
    > Hi I've got the following table
    > CREATE TABLE expenses(
    > id serial NOT NULL PRIMARY KEY,
    > fk_employeeid int4 NOT NULL,
    > fk_projectid int4 NOT NULL,
    > expensedate date NOT NULL,
    > fk_expensetype int4 NOT NULL,
    > notice varchar(2000) NOT NULL,
    > numberofkm numeric,
    > hours numeric,
    > amount numeric,
    > invoicable numeric(1),
    > invoiced numeric(1),
    > responsible int4 NOT NULL,
    > FOREIGN KEY (fk_employeeid) REFERENCES employees (id),
    > FOREIGN KEY (fk_projectid) REFERENCES projects (id),
    > FOREIGN KEY (fk_expensetype) REFERENCES expensetypes (id),
    > FOREIGN KEY (responsible) REFERENCES employees (id));
    >
    > What I do:
    > I create a new Expenses object and register it with DataContext:
    > Expenses newexpensesentry =
    > (Expenses)theDataContext.createAndRegisterNewObject("Expenses");
    > then I fill all the values into that object I've defined relations for
    > the fk_.. references
    >
    > <obj-relationship name="ToEmployees" source="Expenses"
    > target="Employees" toMany="false">
    > <db-relationship-ref source="expenses" target="employees"
    > name="ToEmployees"/>
    > </obj-relationship>
    > <obj-relationship name="ToEmployeesResponsible" source="Expenses"
    > target="Employees" toMany="false">
    > <db-relationship-ref source="expenses" target="employees"
    > name="ToEmployeesResponsible"/>
    > </obj-relationship>
    > <obj-relationship name="ToExpensetype" source="Expenses"
    > target="Expensetypes" toMany="false">
    > <db-relationship-ref source="expenses" target="expensetypes"
    > name="ToExpensetype"/>
    > </obj-relationship>
    > <obj-relationship name="ToProjects" source="Expenses" target="Projects"
    > toMany="false">
    > <db-relationship-ref source="expenses" target="projects"
    > name="ToProjects"/>
    > </obj-relationship>
    >
    > then I set the refenrenced objects i.e.
    > newexpensesentry.ToExpensetype((Expensetypes)theDataContext.registeredObject(new
    > ObjectId(Expensetypes.class, "id",new Integer(1)))));
    > I have the find the referenced object via it's primary key because from
    > a web-interface I only get the primary key for the referenced object and
    > not the object itself
    > I do this for all the referenced objects:
    > then I call theDataContext.commitChanges(Level.WARN);
    > everything works fine, the problem comes now:
    > If I get a list of expenses in a timerange (the Expense object inserted
    > above is under the returned objects), then the newly created Expense
    > objects I get in return from my query has an empty (properties are
    > empty) Projects (relation ToProjects) objects related to it. The strange
    > thing is, that the ExpenseType object that I get over the relation
    > ToExpensetype is ok.
    > If I create a NEW DataContext and rerun the query, again, everything is
    > ok, also the Projects object thas had empty properties before.
    > If I use THE SAME DataContext to insert a new Espense object and the run
    > the query I get problems as described above.
    > Has anyone a idea what the problem could be here?
    > thanks in advance
    > martin
    >



    This archive was generated by hypermail 2.0.0 : Thu Dec 12 2002 - 14:39:47 EST