Duplicate entry

From: Borut Bolčin (cayenn..mail.si)
Date: Thu Feb 17 2005 - 10:02:46 EST

  • Next message: Laszlo Spoor: "RE: [question] ExpressionFactory.notInExp()"

    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?

    Is there or will be any other way?

    At first I was trying to

    Actor actor= (Actor) ctxt.createAndRegisterNewObject("Actor");
    actor.setName("Sharon");
    actor.setLink("http://sharon.stone.com");
    actor.setOid(new Integer(100));
            
    Query query = new InsertQuery(Actor.class);
    int[] count = ctxt.performNonSelectingQuery(query);

    and check the value count if insertion succeded as would be done via
    JDBC API
      int count = psInsertActor.executeUpdate();
    If count is 0 an error occured.

    But the above Cayenne code throws an exception

    java.lang.NullPointerException
        at
    org.objectstyle.cayenne.access.trans.InsertTranslator.prepareLists(InsertTranslator.java:140)
        at
    org.objectstyle.cayenne.access.trans.InsertTranslator.createSqlString(InsertTranslator.java:89)
        at
    org.objectstyle.cayenne.access.trans.QueryAssembler.createStatement(QueryAssembler.java:131)
        at org.objectstyle.cayenne.access.DataNode.runUpdate(DataNode.java:442)
        at
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:356)
        at
    org.objectstyle.cayenne.access.DataDomain.performQueries(DataDomain.java:654)
        at
    org.objectstyle.cayenne.access.DataContext.performQueries(DataContext.java:1408)
        at
    org.objectstyle.cayenne.access.Transaction.performQueries(Transaction.java:179)
        at
    org.objectstyle.cayenne.access.DataContext.performQueries(DataContext.java:1366)
        at
    org.objectstyle.cayenne.access.DataContext.performNonSelectingQuery(DataContext.java:1289)
        at sandbox.cayenne.example1.CreateTable.doIt(CreateTable.java:81)
        at sandbox.cayenne.example1.CreateTable.main(CreateTable.java:39)
    org.objectstyle.cayenne.CayenneRuntimeException: [v.1.1 December 6 2004]
    Query exception.
        at
    org.objectstyle.cayenne.access.QueryResult.nextQueryException(QueryResult.java:203)
        at
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:364)
        at
    org.objectstyle.cayenne.access.DataDomain.performQueries(DataDomain.java:654)
        at
    org.objectstyle.cayenne.access.DataContext.performQueries(DataContext.java:1408)
        at
    org.objectstyle.cayenne.access.Transaction.performQueries(Transaction.java:179)
        at
    org.objectstyle.cayenne.access.DataContext.performQueries(DataContext.java:1366)
        at
    org.objectstyle.cayenne.access.DataContext.performNonSelectingQuery(DataContext.java:1289)
        at sandbox.cayenne.example1.CreateTable.doIt(CreateTable.java:81)
        at sandbox.cayenne.example1.CreateTable.main(CreateTable.java:39)
    Caused by: java.lang.NullPointerException
        at
    org.objectstyle.cayenne.access.trans.InsertTranslator.prepareLists(InsertTranslator.java:140)
        at
    org.objectstyle.cayenne.access.trans.InsertTranslator.createSqlString(InsertTranslator.java:89)
        at
    org.objectstyle.cayenne.access.trans.QueryAssembler.createStatement(QueryAssembler.java:131)
        at org.objectstyle.cayenne.access.DataNode.runUpdate(DataNode.java:442)
        at
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:356)
        ... 7 more

    In source of the InsertTranslator in lines 139 and 140
            Map snapshot = insertQuery().getObjectSnapshot();
            Iterator columnsIt = snapshot.keySet().iterator();
    I guess snapshot is null, hence the above exception.

    How does ctxt.performNonSelectingQuery(query) work? Didn't find any
    examples.

    bye,Borut



    This archive was generated by hypermail 2.0.0 : Thu Feb 17 2005 - 10:03:00 EST