Re: Simple retrieval of an object by PK

From: Mike Kienenberger (mkienen..laska.net)
Date: Thu Jul 15 2004 - 16:47:48 EDT

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

    Jonathan Carlson <Jonathan.Carlso..atun.com> 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.

    If you do a search on the archives, you'll find two or three different ways
    to do this. Easiest solution is to pick one of these methods, and add it to
    a superclass of all of your dataobjects.

    However, because handling meaningless primary keys in java is bad practice,
    last I checked the API doesn't include convenience methods to do this.

    For example, I could go to your above website and trivially hand-craft a url
    that would allow me to access any item in your database directly.

    A better way to do this is to generate a list of all possible items on your
    page (which you've already done to display the page), then have your URL
    only reference the index of the above list rather than the primary key of an
    item in the list. This restricts the user to only those items you've
    already made available to them in the list.

    It also has the side effect of not requiring you to refetch the object from
    the database at this point, but only having to look it up in the list.

    Obviously, there's always tradeoffs. You'll lose the ability to always have
    the "edit this item" link bookmarkable.

    -Mike



    This archive was generated by hypermail 2.0.0 : Thu Jul 15 2004 - 16:47:43 EDT