Re: Duplicate entry

From: Mike Kienenberger (
Date: Thu Feb 17 2005 - 10:51:21 EST

  • Next message: Mike Kienenberger: "Re: [question] ExpressionFactory.notInExp()"

    =?UTF-8?B?Qm9ydXQgQm9sxI1pbmE=?= <> wrote:
    > Hi to all,
    > I read the thread with topic "Unique fields" and was wondering if the
    > current best practice is the one suggested by Michael Gentry:
    > * For your User DbEntity, mark emailAddress as the primary key
    > * For your User ObjEntity, sync with the DbEntity and add emailAddress
    > as an attribute
    > * Generate your classes
    > * In your database:
    > * Create a unique index on emailAddress
    > In your code (the registration page):
    > * Create and register a new User object with your DataContext
    > * Set their emailAddress/etc
    > * commitChanges() and be ready to catch and sort out the exception
    > So if I want to commit an object which has the same PK, I have to parse
    > the CayenneRuntimeException (exception.getCause()) which would be
    > "java.sql.SQLException: Duplicate entry....." and act accordingly?

    If it's a primary key, I'd stick with the auto PK generation so it's handled
    automatically for you.

    If it's another unique field, I recommend setting the unique index on the
    database, but still trying to detect it in your code beforehand.

                    Expression qualifier = ExpressionFactory.likeIgnoreCaseExp("emailAddress",
                    SelectQuery query = new SelectQuery(User.class, qualifier);
                    List results = aDataContext.performQuery(query);
                    if (0 != results.size()) // handle error

    That lessens the likelihood that you'll have a constraint exception.


    This archive was generated by hypermail 2.0.0 : Thu Feb 17 2005 - 10:50:31 EST