RE: [question] Deleting DataObjects using the PK

From: Laszlo Spoor (lspoor_cayenn..otmail.com)
Date: Sat Mar 01 2003 - 19:30:55 EST

  • Next message: Nanik Tolaram: "Simple question"

    Thanks again Graig!

    I will try it first thing tomorrow morning (I thought it had something
    to do with the ObjectId object, but did not figure out: DataObject
    fto=mContext.registeredObject(oid))

    Best regards, Laszlo

    -----Oorspronkelijk bericht-----
    Van: Craig Miskell [mailto:cmiskel..lbatross.co.nz]
    Verzonden: Saturday, March 01, 2003 10:40 PM
    Aan: cayenne-use..bjectstyle.org
    Onderwerp: Re: [question] Deleting DataObjects using the PK

    Assuming that "id" is your pk column, the fact that you can use a
    binaryPathExp to fetch based on that id indicates to me that you have
    the
    id column mapped into your ObjEntity. This is generally dangerous, and
    I'm guessing it's probably the root cause of the problem.

    The solution is to not map the id column in the ObjEntity, and instead
    of
    your fetching code below, do something like:

    ObjectId oid=new ObjectId(Photo.class, "ID", 320);
    // where ID is the DbAttribute name for your pk field, capitilisation
    specific, and 320 is the pk value you're looking for

    DataObject fto=mContext.registeredObject(oid);
    mContext.deleteObject(fto);
    mContext.commitChanges(Level.WARN);

    By working with ObjectId, you kind of encapsulate the access to the pk
    field, without exposing it as a property of the Object.

    Similarly, when you need to find the PK, you can do the following:

    Photo fto;
    //assume fto is initialized
    Integer pk=(Integer)fto.getObjectId().getIdSnapshot().get("ID");
    //again, where ID is the DbAttribute name for the pk field.

    Hope this all helps,
    Craig

     On Sat, 1 Mar 2003, Laszlo Spoor wrote:

    > Hi Guys,
    >
    > When building my website, I need to do a delete, based on my PK (this
    is the
    > only unique key in my table). If I add the Id to my DataMap I can
    delete an
    > instance using:
    >
    > [..]
    > SelectQuery qry = new SelectQuery (Photo.class);
    >
    > Expression qualifier
    > = ExpressionFactory.binaryPathExp(Expression.EQUAL_TO, "id",
    "320");
    >
    > qry.andQualifier(qualifier);
    >
    > Photo fto = (Photo)mContext.performQuery(qry).get(0);
    >
    > mContext.deleteObject(fto);
    > mContext.commitChanges(Level.WARN);
    > [..]
    >
    >
    > However, I am in trouble, whenever I try to create a Photo object,
    using the
    > code:
    >
    > [..]
    > Foto foto = (Foto) mContext.createAndRegisterNewObject("Foto");
    > foto.setToFotoType(fototype);
    > foto.setBestandsnaam("MyFileName.jpg");
    > foto.setUrlFoto("tmp");
    > foto.setUrlThumbnail("tmp");
    > mContext.commitChanges(Level.WARN);
    > [..]
    >
    >
    >
    > WARN QueryLogger: --- will run 1 query.
    >
    > WARN QueryLogger: INSERT INTO dgw_fotos (id, fte_id, bestandsnaam,
    > url_thumbnail, url_foto, id) VALUES (?, ?, ?, ?, ?, ?) [params: NULL,
    2,
    > 'k7s5a.jpg', 'tmp', 'tmp', NULL]
    >
    > WARN QueryLogger: *** error.
    >
    > java.sql.SQLException: General error: Column 'id' specified twice
    >
    > at com.mysql.jdbc.MysqlIO.sendCommand(Unknown Source)
    >
    > at com.mysql.jdbc.MysqlIO.sqlQueryDirect(Unknown Source)
    >
    > at com.mysql.jdbc.Connection.execSQL(Unknown Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at
    org.objectstyle.cayenne.access.DataNode.runUpdate(DataNode.java:412)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:280
    )
    >
    > at
    >
    org.objectstyle.cayenne.access.DataDomain.performQueries(DataDomain.java
    :427)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.jav
    a:873)
    >
    > at
    dgw.handler.CpxFtoHandler.insertPhotoInDb(CpxFtoHandler.java:321)
    >
    > at dgw.handler.CpxFtoHandler.save(CpxFtoHandler.java:375)
    >
    > at
    >
    dgw.controller.ImgCpxFtoAddController.save(ImgCpxFtoAddController.java:4
    1)
    >
    > at _ImgCpxFtoAddAction._jspService(ImgCpxFtoAddAction.jsp:25)
    >
    > at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java:139)
    >
    > at
    oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:348)
    >
    > at
    oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:487)
    >
    > at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:391)
    >
    > at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    >
    > at
    >
    com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChai
    n.java:65)
    >
    > at
    oracle.security.jazn.oc4j.JAZNFilter.doFilter(JAZNFilter.java:292)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestD
    ispatcher.java:606)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.forwardInternal(Servle
    tRequestDispatcher.java:312)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHa
    ndler.java:779)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    264)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    107)
    >
    > at
    >
    com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(Releasab
    leResourcePooledExecutor.java:192)
    >
    > at java.lang.Thread.run(Thread.java:536)
    >
    > WARN DefaultOperationObserver: query exception
    >
    > java.sql.SQLException: General error: Column 'id' specified twice
    >
    > at com.mysql.jdbc.MysqlIO.sendCommand(Unknown Source)
    >
    > at com.mysql.jdbc.MysqlIO.sqlQueryDirect(Unknown Source)
    >
    > at com.mysql.jdbc.Connection.execSQL(Unknown Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at
    org.objectstyle.cayenne.access.DataNode.runUpdate(DataNode.java:412)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:280
    )
    >
    > at
    >
    org.objectstyle.cayenne.access.DataDomain.performQueries(DataDomain.java
    :427)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.jav
    a:873)
    >
    > at
    dgw.handler.CpxFtoHandler.insertPhotoInDb(CpxFtoHandler.java:321)
    >
    > at dgw.handler.CpxFtoHandler.save(CpxFtoHandler.java:375)
    >
    > at
    >
    dgw.controller.ImgCpxFtoAddController.save(ImgCpxFtoAddController.java:4
    1)
    >
    > at _ImgCpxFtoAddAction._jspService(ImgCpxFtoAddAction.jsp:25)
    >
    > at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java:139)
    >
    > at
    oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:348)
    >
    > at
    oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:487)
    >
    > at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:391)
    >
    > at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    >
    > at
    >
    com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChai
    n.java:65)
    >
    > at
    oracle.security.jazn.oc4j.JAZNFilter.doFilter(JAZNFilter.java:292)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestD
    ispatcher.java:606)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.forwardInternal(Servle
    tRequestDispatcher.java:312)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHa
    ndler.java:779)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    264)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    107)
    >
    > at
    >
    com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(Releasab
    leResourcePooledExecutor.java:192)
    >
    > at java.lang.Thread.run(Thread.java:536)
    >
    > WARN QueryLogger: *** error.
    >
    > org.objectstyle.cayenne.CayenneRuntimeException: Raising from query
    > exception.
    >
    > at
    >
    org.objectstyle.cayenne.access.util.ContextCommitObserver.nextQueryExcep
    tion(ContextCommitObserver.java:183)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:288
    )
    >
    > at
    >
    org.objectstyle.cayenne.access.DataDomain.performQueries(DataDomain.java
    :427)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.jav
    a:873)
    >
    > at
    dgw.handler.CpxFtoHandler.insertPhotoInDb(CpxFtoHandler.java:321)
    >
    > at dgw.handler.CpxFtoHandler.save(CpxFtoHandler.java:375)
    >
    > at
    >
    dgw.controller.ImgCpxFtoAddController.save(ImgCpxFtoAddController.java:4
    1)
    >
    > at _ImgCpxFtoAddAction._jspService(ImgCpxFtoAddAction.jsp:25)
    >
    > at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java:139)
    >
    > at
    oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:348)
    >
    > at
    oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:487)
    >
    > at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:391)
    >
    > at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    >
    > at
    >
    com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChai
    n.java:65)
    >
    > at
    oracle.security.jazn.oc4j.JAZNFilter.doFilter(JAZNFilter.java:292)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestD
    ispatcher.java:606)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.forwardInternal(Servle
    tRequestDispatcher.java:312)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHa
    ndler.java:779)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    264)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    107)
    >
    > at
    >
    com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(Releasab
    leResourcePooledExecutor.java:192)
    >
    > at java.lang.Thread.run(Thread.java:536)
    >
    > Caused by: java.sql.SQLException: General error: Column 'id' specified
    twice
    >
    > at com.mysql.jdbc.MysqlIO.sendCommand(Unknown Source)
    >
    > at com.mysql.jdbc.MysqlIO.sqlQueryDirect(Unknown Source)
    >
    > at com.mysql.jdbc.Connection.execSQL(Unknown Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at
    org.objectstyle.cayenne.access.DataNode.runUpdate(DataNode.java:412)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:280
    )
    >
    > ... 20 more
    >
    > WARN QueryLogger: *** transaction rolled back.
    >
    > WARN DefaultOperationObserver: global exception
    >
    > org.objectstyle.cayenne.CayenneRuntimeException: Raising from query
    > exception.
    >
    > at
    >
    org.objectstyle.cayenne.access.util.ContextCommitObserver.nextQueryExcep
    tion(ContextCommitObserver.java:183)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:288
    )
    >
    > at
    >
    org.objectstyle.cayenne.access.DataDomain.performQueries(DataDomain.java
    :427)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.jav
    a:873)
    >
    > at
    dgw.handler.CpxFtoHandler.insertPhotoInDb(CpxFtoHandler.java:321)
    >
    > at dgw.handler.CpxFtoHandler.save(CpxFtoHandler.java:375)
    >
    > at
    >
    dgw.controller.ImgCpxFtoAddController.save(ImgCpxFtoAddController.java:4
    1)
    >
    > at _ImgCpxFtoAddAction._jspService(ImgCpxFtoAddAction.jsp:25)
    >
    > at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java:139)
    >
    > at
    oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:348)
    >
    > at
    oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:487)
    >
    > at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:391)
    >
    > at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    >
    > at
    >
    com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChai
    n.java:65)
    >
    > at
    oracle.security.jazn.oc4j.JAZNFilter.doFilter(JAZNFilter.java:292)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestD
    ispatcher.java:606)
    >
    > at
    >
    com.evermind.server.http.ServletRequestDispatcher.forwardInternal(Servle
    tRequestDispatcher.java:312)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHa
    ndler.java:779)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    264)
    >
    > at
    >
    com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:
    107)
    >
    > at
    >
    com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(Releasab
    leResourcePooledExecutor.java:192)
    >
    > at java.lang.Thread.run(Thread.java:536)
    >
    > Caused by: java.sql.SQLException: General error: Column 'id' specified
    twice
    >
    > at com.mysql.jdbc.MysqlIO.sendCommand(Unknown Source)
    >
    > at com.mysql.jdbc.MysqlIO.sqlQueryDirect(Unknown Source)
    >
    > at com.mysql.jdbc.Connection.execSQL(Unknown Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at com.mysql.jdbc.PreparedStatement.executeUpdate(Unknown
    Source)
    >
    > at
    org.objectstyle.cayenne.access.DataNode.runUpdate(DataNode.java:412)
    >
    > at
    >
    org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:280
    )
    >
    > ... 20 more
    >
    > Debugger disconnected from local process.
    > Process exited with exit code 0.
    >
    >
    > I am probably doing something wrong, but I can't see what. Can anyone
    give
    > me a tip?
    >
    > Thanks in advance, Laszlo Spoor
    >
    > _________________________________________________________________
    > MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
    > http://join.msn.com/?page=features/virus
    >
    >



    This archive was generated by hypermail 2.0.0 : Sat Mar 01 2003 - 19:33:49 EST