FAQ
Hi,

I just started my second cayenne related project and found that I am
doing
the same "workaround" that I did on the first one. Now I wonder if
there is
a better way to do it:

My apps usually have a shopping basket BASKET with a simple relation
to i.e.
BOOKS (via BASKET.isbn -> BOOKS.isbn). Now, I want customers to be
able to add
to the basket without being logged in and so my basket rows might not be
committed to the database unless the user logs in. At that point my
session
which holds all uncommitted basket rows will load in existing basket
rows from
the db and commit.

All this only works if I add the following overwrite to my super
Basket class.
Not a very nice way but proven and stable. Is there a less manual way
to do this?

/Andreas Pardeike



public class Basket extends _Basket
{
// we need to fetch the Book relation manually otherwise transient
baskets
// will fail to resolve getBook()

@Override
public void setISBN(String isbn)
{
super.setISBN(isbn);

Books book = DataObjectUtils.objectForPK(getObjectContext(),
Books.class, isbn);
if(book != null)
setBook(book);
}
}



and here's how I add a basket row:

Basket row = _context.newObject(Basket.class);
row.setISBN(newISBN);
row.setLogin(_login == null ? "" : _login);
...
if(_login != null)
_context.commitChanges();

Search Discussions

  • Marcin Skladaniec at Mar 20, 2008 at 9:20 am
    Hi Andreas

    I might be wrong, but why you just create the Basket and the
    relationship to Book and don't commit the context until the user logs
    in ? :

    on adding item:
    Basket row = _context.newObject(Basket.class);
    row.addToBooks(someBook);

    on successful login:
    row.setUser(someUser);
    _context.commitChanges();

    If you need to store the selected Basket items before the user logs
    in, to allow the user to quit shopping and return later you can do
    that using isbn(s) and cookie(s).

    Marcin


    On 20/03/2008, at 6:55 PM, Andreas Pardeike wrote:
    Hi,

    I just started my second cayenne related project and found that I am
    doing
    the same "workaround" that I did on the first one. Now I wonder if
    there is
    a better way to do it:

    My apps usually have a shopping basket BASKET with a simple relation
    to i.e.
    BOOKS (via BASKET.isbn -> BOOKS.isbn). Now, I want customers to be
    able to add
    to the basket without being logged in and so my basket rows might
    not be
    committed to the database unless the user logs in. At that point my
    session
    which holds all uncommitted basket rows will load in existing basket
    rows from
    the db and commit.

    All this only works if I add the following overwrite to my super
    Basket class.
    Not a very nice way but proven and stable. Is there a less manual
    way to do this?

    /Andreas Pardeike



    public class Basket extends _Basket
    {
    // we need to fetch the Book relation manually otherwise transient
    baskets
    // will fail to resolve getBook()

    @Override
    public void setISBN(String isbn)
    {
    super.setISBN(isbn);

    Books book = DataObjectUtils.objectForPK(getObjectContext(),
    Books.class, isbn);
    if(book != null)
    setBook(book);
    }
    }



    and here's how I add a basket row:

    Basket row = _context.newObject(Basket.class);
    row.setISBN(newISBN);
    row.setLogin(_login == null ? "" : _login);
    ...
    if(_login != null)
    _context.commitChanges();
  • Andreas Pardeike at Mar 20, 2008 at 9:34 am

    On 20 mar 2008, at 10.19, Marcin Skladaniec wrote:

    I might be wrong, but why you just create the Basket and the
    relationship to Book and don't commit the context until the user
    logs in ? :

    on adding item:
    Basket row = _context.newObject(Basket.class);
    row.addToBooks(someBook);

    on successful login:
    row.setUser(someUser);
    _context.commitChanges();

    If you need to store the selected Basket items before the user logs
    in, to allow the user to quit shopping and return later you can do
    that using isbn(s) and cookie(s).
    Maybe I was unclear. BASKET reflects a read/write database where I
    store my basket rows. But BOOKS is on a second database server
    (read-only) and i.e. holds the correlation isbn->title.

    All I want to do is to not store 'title' in basket but have cayenne
    fetch it (preferable lazy) when I need to display the content of
    the shopping basket. I.e. at the time I add a row to the basket in
    my web project, I only have the isbn, nothing else.

    /Andreas Pardeike

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedMar 20, '08 at 8:56a
activeMar 20, '08 at 9:34a
posts3
users2
websitecayenne.apache.org

People

Translate

site design / logo © 2021 Grokbase