Re: Not updating values of FK columns

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Nov 28 2003 - 14:57:04 EST

  • Next message: Andrus Adamchik: "Fwd: table name"

    On Nov 28, 2003, at 9:04 AM, André Luiz do Nascimento Sousa wrote:
    >         I'm having a trouble when updating values of FK columns on a
    > table. The FK columns on table are not updated, but any errors happen.
    > See the following scenario to say if I'm doing something wrong:
    >  
    >         I'm using struts 1.1, Cayenne 1.0.3 and SQL Server 2000.
    >  ..

    >         I've modified the code, bringing from the database the
    > respective record of the lookup table, and mounting the related
    > DataObject. This way, the update works fine, but it's not too much
    > efficient query all the related objects, set them to the update record
    > and then commit.
    >
    > ...
    > // Query the related object
    > ControladorTopicosEscoteiros wTop =
    > ControladorTopicosEscoteiros.getContrTopicosEscoteiros();
    > List wList = wTop.buscarListaRamos(pSessao,
    > pForm.getCodRamoAtualFk31());
    > Ramo wRamo = (Ramo) wList.get(0);
    > wObjPersist.setToRamo(wRamo);
    > ...
    > ctxt.commitChanges(Level.ALL);
    > // The update works
    >  
    >         My question is: Is that right and should I query all the FK
    > related objects? Or is there another way to set values in FK columns
    > when updating?

    Hi Andre Luiz,

    Yes, Cayenne philosophy is to allow objects be objects, i.e. free the
    programmer from direct dealing with PK/FK as much as possible. So using
    relationships as in your last example is the way to go. Now since we
    want to make it as efficient as any other approach, my personal
    preference is to create an application-level shared DataContext and
    prefetch all "lookup" tables in this context (possibly storing them in
    some Map keyed using the meaningful PK or something). Later when there
    is a need to use an object from a lookup table in relationship, you can
    move them to the current (session) context. See
    http://objectstyle.org/cayenne/userguide/datactxt/moving-objects.html
    for details. I believe this has been discussed not so long ago, just
    can't find a link right away.

    Also at some point we may incorporate this approach to lookup tables in
    the core of Cayenne to make it more transparent, but you can use it
    right away with what is already there.

    Andrus



    This archive was generated by hypermail 2.0.0 : Fri Nov 28 2003 - 14:57:09 EST