Re: How to retrieve the PK from the anArtist bean?

From: martin ruff (mruf..imeon.ch)
Date: Mon Dec 30 2002 - 05:08:21 EST

  • Next message: Dirk Olmes: "Re: How to retrieve the PK from the anArtist bean?"

    Hi,
      I just jump in here, cause I had the same problem as Bill, that I had
    to have to PK of an object. I'm explaining now what I am doing and would
    be VERY interested to hear how to do it the proper cayenne way:

    I have a table employees:
    CREATE TABLE employees(
    id serial NOT NULL PRIMARY KEY,
    name varchar(100) NOT NULL,
    mnemonic varchar(30) NOT NULL,
    warningperiod numeric,
    workingpercentage numeric,
    state varchar(1),
    roleid numeric,
    userid varchar(255),
    password varchar(255));
    It's primary key is the id

    now I have a jsp page where I display a list of all employees (employees
    list page)
    the user can click on a employee (which is a link) which directs him to
    a page (employeeEdit) where he can edit the selected user.

    here's my jsp code to show the list
    <logic:iterate id="employeeBO" name="UserList"
    type="com.spectromedia.reporting.bo.EmployeeBO">
       <tr bgcolor="#FFFF88">
         <td bgcolor="#FFFFFF">
                    <b>
                    <html:link paramId="userid" paramName="employeeBO" paramProperty="ID"
    page="/editUser.do"> <bean:write name="employeeBO"
    property="theEmployee.name" /></html:link>
                    </b>
            </td>
            <td bgcolor="#FFFFFF">
                    <bean:write name="employeeBO" property="theEmployee.userid" />
            </td>
         <td bgcolor="#FFFFFF">
                   <bean:write name="employeeBO" property="theEmployee.mnemonic" />
            </td>
       </tr>
    </logic:iterate>

    the EmployeeBO is just a wrapper for the Employees object (the generated
    extended CayenneDataObject), its attribute theEmployee is of type
    CayenneDataObject
    ID is the PK which I get with getObjectId().getIdSnapshot() ..., which I
    pass as parameter to the employeeEdit page.

    The problem now is: On the employeeEdit page I have to identify the
    employee the user selected in the list, therefor I submit it's ID (the
    PK) to get the appropriate selected user on the employeeEdit page.
    In a swing application I would show the user a list of Employees objects
    (of type CayenneDataObject). when the user selects an employee I would
    pass the Employees object to the employeeEdit page, so I would not have
    to access its PK. I would ALLWAYS work with objects.
    But in a web app, I cannot put Employees objects in the employees list
    page (jsp page) and pass a Employees object as request parameter to the
    employeeEdit page. So in this web-context what would be the right
    cayenne way to solve the problem described WITHOUT accessing the
    employees PK?
    thanks martin
    > Just to add to Dirks comments: If you *really* *really* *really* need
    >
    > to access the pk and don't want to map it in the ObjEntity (which
    > is
    > a >Good Thing (tm)), then you can ask any DataObject for it's
    > ObjectId (getObjectId() is the method), from which you can get the
    > IdSnapshot. the snapshot is a map, keys being the pk column name(s),
    > value being the pk value.

    > You can even pass a manually constructed ObjectId to a DataContext to
    > >get the object back (assuming the risk of an exception if the
    > corresponding row doesn't exist in the db).

    > But can I just reiterate what Dirk said about pk/fk's being
    > relational artifacts. They really do just exist for the convenience
    > of the database (nice, small, easily compared identifiers for a
    > particular row). Even using them to identify objects in the database
    > is dubious, and if there's any other column at all that is unique
    > (or should be unique), then use that. It will save you a *LOT* of
    > hassle long term. Believe me. Been there, done that, bought the
    > t-shirt.

    Craig

      On Mon, 30 Dec 2002, Fan, Bill (AP - Australia) wrote:

    > Hi,
    >
    > I'm new to Cayenne. I've successfully set up the cayenne-web-app with
    >
    >
    tomcat
    > 4.1.17. My question is how to retrieve the ARTIST_ID in order to use
    it for
    > the "add painting" link? The exampe is using the artist's name for
    > this link. In the struts app I'm working on, I need to use the
    > primary key to associate the records.
    >
    > I noticed that there is no getter method for any PK column in all the
    > java class file generated.
    >
    >
    > Thanks! Bill
    >
    >
    >
    >



    This archive was generated by hypermail 2.0.0 : Mon Dec 30 2002 - 05:07:37 EST