[Fwd: Re: Newbie: Tapestry 2.4 and cayenne1.0a6]

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Mar 07 2003 - 13:40:44 EST

  • Next message: martin ruff: "cayenne memory behaviour"

    Oh, just checked out the tutorial code. It needs to be revisited...

    One possible fix that I can suggest, is on AddArtistPage remove
    initialize method and instead override Tapestry's
    AbstractPage.beginResponse to create artist (store) like that:

    // clean context of any dangling changes
    ctxt.rollbackChanges();

    if(store == null)
       // create new store, if it wasn't setup by the previous page
       store = (Store)ctxt.createAndRegisterNewObject("Store");
    }

    This should fix the problem.

    We will work on cleaning up tutorials for the next release.

    Andrus

    > Hi,
    >
    > I actually just copied the AddArtistPage and modified that to create a
    > Store instance by renaming class names and field names. So the
    > SaveStoreAction (which I've shown below) is what creates the Store in
    > the first place. Then, control goes back to the BrowseStorePage which
    > shows all the instances as it should (it is a copy of
    > BrowseArtistPage.java). On this page I have an EditStoreAction link
    > which passes control back to the same AddStorePage that created the
    > Store instance in the first place. The editStoreAction merely sets
    > the Store instance in the AddStorePage to the one selected on the
    > browse page before it passes control over. The data shows correctly
    > in the AddStorePage and you can edit it, but with the code as shown it
    > creates a new instance.
    >
    > I tried it before without the TRANSIENT check, but then it gave a
    > database error saying that I'm trying to save a duplicate copy of the
    > primary key, so I assumed that I should not try to register an
    > instance again.
    >
    > Thanx
    > reinier
    >
    > On Fri, 2003-03-07 at 01:59, Andrus Adamchik wrote:
    >> Hi Reinier,
    >>
    >> In the code snip you sent I can't see where you actually create a new
    >> Store object. I suspect you may create (or rather register) it twice
    >> somehow. Please send this piece of code.
    >>
    >> In any event, whenever you create an object, it is much cleaner to
    >> use the code like this (if you don't yet):
    >>
    >> Store store = (Store)ctxt.createAndRegisterNewObject("Store");
    >>
    >> This should be enough to both create an instance and register it with
    >> DataContext (just like the name of the name of the method implies).
    >> There is no need to register the object after that, later TRANSIENT
    >> check is redundant.
    >>
    >> Andrus
    >>
    >>
    >> > Okay, the answer night be in the mailing list, but if so I could
    >> not
    >> recognise it with my limited knowledge.
    >> >
    >> > The problem is: I'm trying to take the Cayenne-Tapestry tutorial,
    >> and adding an edit page to it. I've modelled a new Store class on
    >> the BrowseArtistPage class, and that works very well. Then I added a
    >> new editStoreLink (as an ActionLink) that calls the AddStorePage
    >> (modelled on AddArtistPage), like so:
    >> >
    >> > public void editStoreAction(IRequestCycle cycle) {
    >> > AddStorePage nextPage = (AddStorePage)
    >> cycle.getPage("AddStorePage");
    >> >
    >> > nextPage.setStore(store);
    >> >
    >> > cycle.setPage(nextPage);
    >> > }
    >> >
    >> > This sets up the AddStorePage with the correct values when it
    >> opens,
    >> and you can edit them as you like. I commit the changes as follows:
    >> >
    >> > public void saveStoreAction(IRequestCycle cycle) {
    >> > Visit visit = (Visit) this.getPage().getVisit();
    >> > DataContext ctxt = visit.getDataContext();
    >> >
    >> > if (!assertNotNull(store.getName()) ) {
    >> > appendHtmlToErrorMessage("You must provide a name.");
    >> > }
    >> >
    >> > if (getHasErrorMessage()) return;
    >> >
    >> > if (store.getPersistenceState() == PersistenceState.TRANSIENT) {
    >> > ctxt.registerNewObject(store);
    >> > }
    >> >
    >> > // commit to the database
    >> > ctxt.commitChanges();
    >> >
    >> > BrowseStoresPage nextPage =
    >> > (BrowseStoresPage) cycle.getPage("BrowseStoresPage");
    >> >
    >> > cycle.setPage(nextPage);
    >> > }
    >> >
    >> > I check for the PersistenceState so that an existing object does
    >> not
    >> get registered again. The idea is that the same page can be used to
    >> create and register new objects, as well as edit (without
    >> registering) existing objects. However, when I commit the changes, I
    >> get a duplicate entry with a new ObjectId.
    >> >
    >> > Clearly, I'm still not quite getting something. Please help . . .
    >> >
    >> > reinier
    >> >
    >> > PS this looks to be a VERY nice combination for developing web apps
    >> - I think it will work very well when I understand it a bit better.



    This archive was generated by hypermail 2.0.0 : Fri Mar 07 2003 - 13:40:44 EST