Re: Object creation/update:

From: Fabricio Voznika (fabricio.voznik..artmouth.EDU)
Date: Tue Aug 12 2003 - 12:04:50 EDT

  • Next message: Tore Halset: "Re: problem using JNDIDataSource"

    Hi Adrian,

        You're almost right. You need to query the database to check whether
    the user already exists, if it doesn't, you just create one as you said.
    If it does exist, the object will be returned in the query, so there's
    no need to call objectFromDataRow(), here is an example:

            String alias;
            DataContext ctx;
            ...
            Expression expr = ExpressionFactory.matchExp("userAlias", alias);
            SelectQuery query = new SelectQuery(User.class, expr);
            List res = ctx.performQuery(query);
            User user;
            if(res.isEmpty()) {
                user = (User)ctx.createAndRegisterNewObject("User");

            } else {
                assert res.size() == 1 : "Something really bad happened.";

                user = (User)res.get(0);
            }
            // Set attributes...
            ctx.commitChanges();

        It helps if you think users as Object, real entities, rather than
    rows in a table. So you're either setting an exiting User or creating a
    new one. It shouldn't matter to your application (ideally of course :-)
    ) if these objects are being stored as a row in a table, or being
    serialized to a file. They are being persisted "somehow" and you have
    some methods to find them.

    I hope it helps,
    Fabricio.

    Adrian wrote:

    > Sorry if this a very basic question, but I dont know what is the best
    > way to solve it.
    > I am developening a system that register and updates users:
    > Someone enters data in one form...if this data corrsponds to an
    > existing user, it updates the row if not it cretes a new row entry in DB.
    > The question is..Cayenne manages by itself the Pk, so: how can
    > indicate Cayenne that it must check for another field (i.e.:
    > userAlias) in order to recognize if this data belongs to an existing
    > user that must be updated or a new one to be added?
    > Must I make a select query trying to find this 'userAlias'?...then if
    > this object exists...get it using
    > "DataContext.objectFromDataRow()"...update the field and commit changes...
    > Or if it doesn't
    > exist...dataContext.createAndRegisterNewObject("User")...set its
    > attributes and commit changes.
    > Is this the correct aproach...or there is a simpler way to get this
    > behaviour?
    >
    > Thx. in advance.
    >
    > ------------------------------------------------------------------------
    > Do you Yahoo!?
    > Yahoo! SiteBuilder
    > <http://us.rd.yahoo.com/evt=10469/*http://sitebuilder.yahoo.com> -
    > Free, easy-to-use web site design software

    -- 
    Fabricio Voznika
    Senior Programmer/Analyst
    Administrative Computing
    Dartmouth College
    Phone: 603-646-2007
    



    This archive was generated by hypermail 2.0.0 : Tue Aug 12 2003 - 11:58:57 EDT