Re: Simple retrieval of an object by PK

From: Eric Schneider (eri..entralparksoftware.com)
Date: Thu Jul 15 2004 - 16:56:21 EDT

  • Next message: Andrus Adamchik: "Re: Simple retrieval of an object by PK"

    Jonathan,

    Yeah, unfortunately sometimes things don't jump out at you as much as
    you'd like them to. I'm dealing with the same thing trying to learn
    Hibernate right now. :-\

    Easiest way is to do something like this:

    DataObject object = dataContext.registeredObject(new
    ObjectId(MyClass.class "MY_PK_COLUMN", myPk));

    The registeredObject() method will....

    1) check to see if your object is already in the object graph, if so
    return it.
    2) if not, fetch the object from the database, return it.

    In scenarios when you always want to fetch from the database, using
    dataContext.refetchObject() is probably your best bet.

    Cheers,
    Eric

    On Jul 15, 2004, at 4:17 PM, Jonathan Carlson wrote:

    > I gravitate towards powerful tools that make simple things simple and
    > hard things possible. I know that Cayenne is in this category of
    > tools, but I don't see any way to simply and quickly retrieve an
    > instance by it's primary key -- something that should be very easy and
    > concise.
    >
    > For example, when doing web apps this seems like a common use case:
    > 1) Display a list of instances that each have a link to an edit page.
    > (For example, each link would include a ?pk=999 request parameter
    > which is not visible on the HTML page.)
    > 2) When the link is clicked, the action class grabs the pk request
    > param and does a quick lookup of the obect before rendering the edit
    > page.
    >
    > There must be a straightforward simple API to get an object by its PK
    > without having to do 6 lines of code to setup a query, expression,
    > params, and execution. It's really easy to grab all instances of an
    > ObjEntity, why shouldn't it be just as easy to grab one instance?
    >
    > (BTW, I think DataContext.refetchObject(ObjectId oid) is on the right
    > track towards the simplicity I'm looking for, but that doesn't appear
    > to fill the need)
    >
    > Thanks in advance for any tips you might have in this area. It's
    > possible I just missed something, but I think I've read all the
    > relevant documentation.
    >
    > Thanks again,
    >
    > Jonathan
    >
    >
    >
    > ***********************************************************************
    > **********
    > This email and any files transmitted with it are confidential and
    > intended solely for the use of the individual or entity to whom they
    > are addressed. If you have received this email in error please notify
    > the system manager.
    >
    > Katun Corporation -- Celebrating 25 Years of Service
    > www.katun.com
    > ***********************************************************************
    > **********
    >



    This archive was generated by hypermail 2.0.0 : Thu Jul 15 2004 - 16:57:48 EDT