Re: How to enforce Delete before Insert?

From: Jürgen Saar (Juerge..Saar.org)
Date: Fri Apr 28 2006 - 03:40:26 EDT

  • Next message: Marcin Skladaniec: "re-fething object from the database"

    This problem is well known to me.
    We do it the same way.
    It is much easyier because it is not neccessary
    to analyze for changes.

    Cayenne does not commit the changes in a fifo-logic.
    To solve this we use a separate context
    for delete and insert ...

    --- Juergen ---

    -----Ursprüngliche Nachricht-----

    Hi,

    I have a Product & a ProductRelation tables. When the ProductRelation

    records for a product are managed in a web page, I'd like to delete the
    exiting records in the ProductRelation table for this product first, then
    insert the new one.

    I'm having trouble to do this with the following code. I got the "Cannot

    insert duplicate key in object ..." db error. The reason appears that the
    DELET statements appear after the INSERT statements from what I can see in
    the QueryLogger output. Could someone please help? I'm using Cayenne 1.2B2.

    The code:
    =========
            public void saveProductRelation(Product product, String
    relatedProductId[], Type relationType)
            {
                    // delete the existing records
                    
    log.debug("product.getProductRelationArray().size()=" +
    product.getProductRelationArray().size());

    threadDataContext().deleteObjects(product.getProductRelationArray());

            if (relatedProductId.length
    > 0)
            {
                    for
    (int i=0; i<relatedProductId.length; i++)
                {
                    log.debug("relatedProductId["
    + i + "]=" +
    relatedProductId[i]);

                    ProductRelation
    o = new ProductRelation();
                    threadDataContext().registerNewObject(o);
                    o.setToProduct(product);
                    o.setToProduct1((Product)
    objectForPK(Product.class, new
    Integer(relatedProductId[i].trim())));
                    o.setToType(relationType);

                    product.addToProductRelationArray(o);
                }
            }

                    commitChanges();

            }

    The QueryLogger output:
    =======================
    dao.ProductDaoImpl.saveProductRelation(ProductDaoImpl.java:713) -
    <relatedProductId[0]=9>
    cayenne.access.QueryLogger.logQueryStart
    (QueryLogger.java:459) - <--- will
    run 2 queries.>
    cayenne.access.QueryLogger.logBeginTransaction(QueryLogger.java:413) - <---
    transaction started.>
    cayenne.access.QueryLogger.logQuery(QueryLogger.java
    :336) - <INSERT INTO
    dbo.ProductRelation (ProductId, RelatedProductid, TypeId) VALUES (?, ?, ?)>
    cayenne.access.QueryLogger.logQueryParameters(QueryLogger.java:358) -
    <[bind: 242, 9, 16]>
    cayenne.access.QueryLogger.logUpdateCount
    (QueryLogger.java:404) - <===
    updated 1 row.>
    cayenne.access.QueryLogger.logQuery(QueryLogger.java:336) - <DELETE FROM
    dbo.ProductRelation WHERE ProductId = ? AND RelatedProductid = ? AND TypeId
    = ?>

    cayenne.access.QueryLogger.logQueryParameters(QueryLogger.java:358) -
    <[bind: 242, 222, 16]>

    Many thanks in advace!

    Bill

    </textarea></td>
                    </tr>
            </table>
                    
                            
            
            
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                    <tr>
                            <td colspan="4"><img src="//img.web.de/v/p.gif" width="1" height="15" border="0" alt="" /></td>
                    </tr>
                    <tr>
                            
                            <td valign="top" class="a"><input class="a" type="submit" name="rv_do_correctmsg" value="Rechtschreibung"></td>
                            <td width="10"><img src="//img.web.de/v/p.gif" width="10" height="1" border="0" alt="" /></td>
                            <td class="b" valign="top"><select class="b" name="rv_lang">
                                    
                                                            <option value="deutsch" selected="selected">Deutsch</option>
                                            
                                                            <option value="british" >Englisch (England)</option>
                                            
                                                            <option value="english" >Englisch (Amerika)</option>
                                            
                                                            <option value="francais" >Französisch</option>
                                            
                                                            <option value="italian" >Italienisch</option>
                                            
                                                            <option value="espanol" >Spanisch</option>
                                            
                                                            <option value="catala" >Katalanisch</option>
                                            
                                                            <option value="portugues" >Portugiesisch (Portugal)</option>
                                            
                                                            <option value="br" >Portugiesisch (Brasilien)</option>
                                            
                                                            <option value="dansk" >Dänisch</option>
                                            
                                                            <option value="nederlands" >Holländisch</option>
                                            
                                                            <option value="svenska" >Schwedisch</option>
                                            
                                                            <option value="norsk" >Norwegisch</option>
                                            
                                                            <option value="czech" >Tschechisch</option>
                                            
                                                            <option value="polish" >Polnisch</option>
                                            
                                                            <option value="esperanto" >Esperanto</option>
                                            
                                                            <option value="slovak" >Slowakisch</option>
                                            </select></td>
                                    
                                            <td width="100%"> </td>
                                    
                    </tr>
            </table>

    <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                    <td height="15"></td>
            </tr>
    </table>

            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                    <tr>
                            <td class="b" bgcolor="#D6E3FE" height="17"> <b>Anlage zu dieser E-Mail hinzufügen</b></td>
                    </tr>
            </table>
            <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE">
                                            <tr>
                                                    <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                                                    <td class="b"><nobr><b>1. Schritt:</b> Datei auswählen</nobr></td>
                                                    <td width="8"><img src="//img.web.de/v/p.gif" width="8" height="1" border="0" alt="" /></td>
                                                    <td class="a" width="100%" colspan="3"><input class="a" name="rv_filename" type="file" size="38" /></td>
                                                    <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                                            </tr>
                                            <tr>
                                                    <td colspan="5" height="5"><img src="//img.web.de/v/p.gif" width="1" height="5" border="0" alt="" /></td>
                                            </tr>
                                            <tr>
                                                    <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                                                    <td class="b"><b>2. Schritt:</b> Datei hochladen</td>
                                                    <td width="8"><img src="//img.web.de/v/p.gif" width="8" height="1" border="0" alt="" /></td>
                                                    <td class="b"><input class="a" type="submit" name="rv_do_uploadatt" value="Hochladen" onClick="document.msgform.action='/online/logic/action.htm?si=1wlyZ.1fzngR.1ppQMc.c**&rv_do_uploadatt=yes&rv_mid=272890087';"/></td>
                                                    <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                            <td align="right" class="b"><b><nobr><a href="/online/hilfe/jump/?si=1wlyZ.1fzngR.1ppQMc.c**&goto=/freemail/e-mail/anlagen/hinzufuegen/" target="_blank" onClick="autoPopup('url=/online/hilfe/jump/?si=1wlyZ.1fzngR.1ppQMc.c**&goto=/freemail/e-mail/anlagen/hinzufuegen/','name=hilfe,width=752,height=500,scrollbars=yes, resizable=no'); return false;">Komfort-Upload</a></nobr></b></td>
                                                    <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                        </tr>
                                    </table>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                </td>
                    </tr>
            </table>

            <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#ffffff">
                <tr>
                    <td class="b"><span style="color: red">Tipp:</span> <a href="/online/jump/dd/?si=1wlyZ.1fzngR.1ppQMc.c**&mc=freemai..l@emailsize@schreiben.digita..lub@upgradefm&dienst=digitaledienste&goto=/freemail/club/emailsize_ttc/">Anlagen bis 50 MB verschicken</a></td>
                </tr>
            </table>
        
    <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                    <td height="15"><a name="box1"></a></td>
            </tr>
    </table>
    <div id="d10" style="display: block;">
            <table cellpadding="0" cellspacing="0" width="100%" border="0" bgcolor="#D6E3FE">
                    <tr>
                            <td class="b" height="17" onclick="showdiv('11');hidediv('10');" style="cursor:hand;"> <b>Empfänger per SMS auf diese Nachricht hinweisen</b></td>
                            <td class="a" align="right" onclick="showdiv('11');hidediv('10');" style="cursor:hand;"><a href="#box1" onclick="showdiv('11');hidediv('10');"><img src="//img.web.de/v/mail/0/iconsneu/ico_aus.gif" width="17" height="17" border="0" alt="" /></a></td>
                    </tr>
            </table>
    </div>
    <div id="d11" style="display: none;">
            <table cellpadding="0" cellspacing="0" width="100%" border="0" bgcolor="#D6E3FE">
                    <tr>
                            <td class="b" height="16" onclick="showdiv('10');hidediv('11');" style="cursor:hand;"> <b>Empfänger per SMS auf diese Nachricht hinweisen</b></td>
                            <td class="a" align="right" onclick="showdiv('10');hidediv('11');" style="cursor:hand;"><a href="#box1" onclick="showdiv('10'); hidediv('11');"><img src="//img.web.de/v/mail/0/iconsneu/ico_ein.gif" width="17" height="16" border="0" alt="" /></a></td>
                    </tr>
            </table>
            <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td valign="top">
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                                    <table cellpadding="2" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE">
                                            <tr>
                                                    <td class="b" align="right"><nobr> <b><a href="javascript:ShowKurier('kurier')">Handy-Nummer:</a></b> </nobr></td>
                                                    <td width="100%" class="b">
                                                            <input type="text" size="35" style="width:100%;" maxlength="255" name="rv_smsnumber" value="" />
                                                            <input type="hidden" name="rv_adrbuch" value="0" />
                                                            <input type="hidden" name="remsg_mail2sms" value="" /></td>
                                                    <td width="40" class="b" align="right"><nobr><a href="javascript:neues_fenster('/online/popup/tarife/frameset.htm?si=1wlyZ.1fzngR.1ppQMc.c**',588,375);"><b>Tarife & Club-Rabatte</b></a> </nobr></td>
                                            </tr>
                                    </table>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                            </td>
                    </tr>
            </table>
    </div>

    <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                    <td height="15"><a name="box2"></a></td>
            </tr>
    </table>
    <div id="d12" style="display: block;">
            <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td class="b" height="17" onclick="showdiv('13');hidediv('12');" style="cursor:hand;"> <b>Sicherheit</b></td>
                            <td class="b" align="right" onclick="showdiv('13');hidediv('12');" style="cursor:hand;"><a href="#box2" onclick="showdiv('13'); hidediv('12');"><img src="//img.web.de/v/mail/0/iconsneu/ico_aus.gif" width="17" height="17" border="0" alt="" /></a></td>
                    </tr>
            </table>
    </div>
    <div id="d13" style="display: none;">
            <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td class="b" height="16" onclick="showdiv('12');hidediv('13');" style="cursor:hand;"> <b>Sicherheit</b></td>
                            <td class="a" align="right" onclick="showdiv('12');hidediv('13');" style="cursor:hand;"><a href="#box2" onclick="showdiv('12'); hidediv('13');"><img src="//img.web.de/v/mail/0/iconsneu/ico_ein.gif" width="17" height="16" border="0" alt="" /></a></td>
                    </tr>
            </table>
            <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td valign="top">
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                                    <table cellpadding="2" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE">
                                            <tr>
                                                    <td class="b" width="150"> <b>Digitale Unterschrift:</b></td>
                                                    <td class="b" width="25">
                                                    
                                                            <input type="checkbox" name="rv_sign" value="on" />
                                                    </td>
                                                    <td class="b">diese Nachricht mit meinem Zertifikat digital unterschreiben</td>
                            <td><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td>
                                            </tr>
                                            <tr>
                                                    <td class="b"> <b>Verschlüsselung:</b></td>
                                                    <td class="b">
                                                            
                                                                            <input type="checkbox" name="rv_enc" value="on" />
                                                            </td>
                                                    <td class="b">diese Nachricht verschlüsseln
                                                    
                                                            <select class="b" name="rv_strength">
                                                                    <option VALUE="128">normal (128 Bit)</option>
                                                                    <option VALUE="des">mittel (168 Bit)</option>
                                                                    <option VALUE="aes">stark (256 Bit)</option>
                                                            </select>
                                                    
                                                    
                                                    
                                                    </td>
                            <td align="right" class="b"><a href="http://pc-sicherheit.web.de/" target="_blank">Sicherheit bei WEB.DE</a> <br /></td>
                                            </tr>
                                    </table>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                            </td>
                    </tr>
            </table>
    </div>

    <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                    <td height="15"><a name="box3"></a></td>
            </tr>
    </table>

    <div id="d14" style="display: block;">
            <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td class="b" height="17" onclick="showdiv('15');hidediv('14');" style="cursor:hand;"> <b>Meine Visitenkarte</b></td>
                            <td class="a" align="right" onclick="showdiv('15');hidediv('14');" style="cursor:hand;"><a href="#box3" onclick="showdiv('15'); hidediv('14');"><img src="//img.web.de/v/mail/0/iconsneu/ico_aus.gif" width="17" height="17" border="0" alt="" /></a></td>
                    </tr>
            </table>
    </div>
    <div id="d15" style="display: none;">
            <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td class="b" height="16" onclick="showdiv('14');hidediv('15');" style="cursor:hand;"> <b>Meine Visitenkarte</b></td>
                            <td class="a" align="right" onclick="showdiv('14');hidediv('15');" style="cursor:hand;"><a href="#box3" onclick="showdiv('14'); hidediv('15');"><img src="//img.web.de/v/mail/0/iconsneu/ico_ein.gif" width="17" height="16" border="0" alt="" /></a></td>
                    </tr>
            </table>
            <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#D6E3FE">
                    <tr>
                            <td>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE">
                                            
                            <tr>
                                                            <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                                                        <td width="38"><img src="//img.web.de/v/mail/iconsneu/ico_visitenkarte_kl.gif" border="0" width="38" height="24" alt="" /></td>
                                                        <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                                                            <td class="b" width="20"><input type="checkbox" class="a" name="rv_do_vcard" value="vcard" /></td>
                                                            <td width="8"><img src="//img.web.de/v/p.gif" width="8" height="1" border="0" alt="" /></td>
                                                        <td class="b"><nobr>Meine Visitenkarte anhängen</nobr></td>
                                <td width="100%" align="right"><input type="submit" class="a" style="width:200px;" name="editvcard" value="Meine Visitenkarte bearbeiten" /></td>
                                                            <td width="5"><img src="//img.web.de/v/p.gif" width="5" height="1" border="0" alt="" /></td>
                            </tr>
                                                    
                                                            
                                    </table>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#F5F8FE"><tr><td height="7"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt="" /></td></tr></table>
                            </td>
                    </tr>
            </table>
    </div>

                    <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                    <td colspan="3" height="15"></td>
                            </tr>
                    </table>
            
                    <table border="0" cellspacing="0" cellpadding="0">
                    
                            <tr>
                                    <td colspan="3">
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td class="a" valign="middle"><input class="a" type="submit" name="rv_do_send" value="Senden" accesskey="s" /></td>
                                <td width="10"></td>
                                <td class="a" valign="middle"><input class="a" type="submit" name="rv_do_savedraft" value="Als Entwurf speichern" style="width:140px;" /></td>
                            </tr>
                        </table>
                    </td>
                            </tr>
                    
                    </table>
            
                    <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                    <td height="15"></td>
                            </tr>
                    </table>
                    
                            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                                    <tr>
                                            <td>
                                                    
                                            </td>
                                    </tr>
                            </table>
                    
            </form>
            <script language="JavaScript" type="text/javascript"><!--
                    document.msgform.rv_js.value = "yes";
          // var base = document.msgform;
            //alert(base.getElementsByTagName('input').length);
            //--></script>

                    <table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">
            
            <tr>
                    <td>
    <script type="text/javascript" src="//img.web.de/v/c/popup3.js"></script>
    <center>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr><td height="15"></td></tr>
        <tr><td height="1" bgcolor="#888888"><img src="//img.web.de/v/p.gif" width="1" height="1" border="0" alt=""></td></tr>
        <tr><td height="10"></td></tr>
    </table>
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
            <td class="a" align="center" valign="middle"><a href="http://r.web.de/f/common/foot/t/hp/impressum/u/d2ViLmRlL0ltcHJlc3N1bS8_/c/Um2OR$De1Tl2" target="_blank" onclick="autoPopup('url=http://r.web.de/f/common/foot/t/hp/impressum/u/d2ViLmRlL0ltcHJlc3N1bS8_/c/Um2OR$De1Tl2','name=headset,width=500,height=451,scrollbars=no, resizable=no'); return false;">Impressum</a> - <a href="http://r.web.de/f/common/foot/t/webdede/home/u/d2ViZGUuZGU_/c/PBwC1YdG0EdC" target="_blank">WEB.DE GmbH</a> - <a
            href="http://r.web.de/f/common/foot/t/press/home/u/cHJlc3NlLndlYi5kZQ__/c/AqgrH5PGYkQh" target="_blank">Presse</a> - <a
            href="http://r.web.de/f/common/foot/t/advertising/home/u/YWR2ZXJ0aXNpbmcud2ViLmRlLw__/c/NDILSHiLGXWC" target="_blank">Werbung</a> - <a
            href="http://r.web.de/f/common/foot/t/jobs/home/u/am9icy53ZWIuZGU_/c/rsI2zWnjWwrY" target="_blank"><b>Jobs</b></a></td>
    </tr>
    </table>
    </center>
    <br>

    </td>
            </tr>
    </table>
    </BODY></HTML>
    _______________________________________________________________
    SMS schreiben mit WEB.DE FreeMail - einfach, schnell und
    kostenguenstig. Jetzt gleich testen! http://f.web.de/?mc=021192



    This archive was generated by hypermail 2.0.0 : Fri Apr 28 2006 - 03:41:07 EDT