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

From: Dirk Olmes (dirk.olme..mx.de)
Date: Tue Dec 31 2002 - 02:38:33 EST


> Hi Dirk & Martin,
>
> Very cool idea!! Thanks very much for that!
>
> > Sure, while a unique identifier for a (CayenneData)Object already
> exist, why
> > not use it?
> How do you use this unique identifier(i.e the hashCode() value) to
> retrieve the Artist object from the data context?

Argh! This was meant to be more of a rethorical question. What I really
was trying to make clear is that you have to distinguish between the
different models you're using:

- in the Object model, an instance can be uniquely identified by its
hashCode().
- in the relational model you have to specify the unique identifier as
primary key.

I guess the difficulties arise from the fact that one wears different
hats in the development cycle:

- mapping the relational model to the object model
- using the object model to implement the web app

When you wear the mapper's hat you have to worry about primary keys
etc. To maintain consistency across the object/relational border you
have to bring over the primary key (value) to the o/r mapping layer,
otherwise you would lose track of which database row you're updating.

When you wear the web app implementor's hat you don't want to worry at
all about where the objects come from. You'll just use the object
model. Nothing else.

> What I'm trying to do is exactly the same as Martin does - edit(hence
> to update) the artist record. However, I'm trying to avoid to get to
> the PK value as you suggested, I passed the
> anArtist.getObjectId().hashCode() instead.

Why do you mess with the objectId? A quick quote from the javadoc
reveals the real purpose of that class:

> Each data object has an id uniquely identifying it. ObjectId concept
> corresponds to a primary key concept in the relational world. Such id
> is needed to implement object uniquing and other persistence layer
> functions.

You should take the last sentence for serious: the ObjectId should be
considered an implementation detail of the o/r mapping layer.

> In the artistBrowsePage.jsp, I added a hyperlink like this,
>
> <a
> href="editArtist.do?objectIdHashCode=<%=anArtist.getObjectId().hashCode
> ()%>">edit artist</a>
>
> When this link is clicked, the control will be passed to the
> EditArtistAction.java(a new class I'm playing with). In this class,
> how do I use the objectIdHashCode value passed from the reuqest object
> to retrieve the corresponding Artist object?

You're almost there! Just don't use the hashCode() of the objectId, use
the hashCode of the Artist itself. Cayenne makes sure (with the help of
the objectId) that two rows with the same primary key will be
represented by exactly the same instance of Artist. This is in the same
DataContext, of course.

I've just whipped together a quick testcase to assert that I'm not just
talking nonsense here. I didn't even use cayenne, just instantiated
dummy Artists in memory and it works. For large collections the linear
search for the hashCode in a List is not optimal but with the help of a
Map this can be overcome.

-dirk



This archive was generated by hypermail 2.0.0 : Tue Dec 31 2002 - 02:39:08 EST