You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Bradford Pielech <br...@alphatech.com> on 2004/06/11 22:24:57 UTC

deleteByQuery does not cascade deletes

Hello again:

	I am writing some JUnit tests now and as part of the setup, I want to make 
sure the DB is empty, so I wrote a method to delete all relevant objects 
and their related objects:

----------------
public boolean deleteAllObjects(String classNameOfObject) {
     boolean returnValue = true;
     Class exampleClass = Class.forName(classNameOfObject);
     Query query = QueryFactory.newQuery(exampleClass,
                                         QueryByCriteria.CRITERIA_SELECT_ALL);
     PersistenceBroker pbroker = PersistenceBrokerFactory.
         defaultPersistenceBroker();
       pbroker.beginTransaction();
       pbroker.deleteByQuery(query);
       pbroker.commitTransaction();
}
-------------------

However, when I run this, only the objects from the Primary class are 
deleted (see below) and not any associated ReferencedObjects.  This was 
confirmed by looking at the p6spy logI changed the code to deleteAll by 
first retrieving all objects of the type and then calling delete on each:

----------
public boolean deleteAllObjects(String classNameOfObject) {
        PersistenceBroker pbroker = 
PersistenceBrokerFactory.defaultPersistenceBroker();
     try {
       List objects = getObjectsOfType(classNameOfObject);      //uses 
getCollectionByQuery and CRITERIA_SELECT_ALL
       pbroker.beginTransaction();
       for (Iterator i = objects.iterator(); i.hasNext(); ) {
         pbroker.delete(i.next());
       }
       pbroker.deleteByQuery(query);
       pbroker.commitTransaction();
------------

And this version successfully deleted all objects and their associations 
(again confirmed by p6spy).  Now I know the mapping file is correct, 
otherwise the 2nd version would have failed.  I included relevant sections 
of it below, just in case.

Any ideas what I am doing wrong?

thanks,
Brad

--------------------
<class-descriptor
     class="PrimaryClass"
     table="table1"
 >
<reference-descriptor
         name="tasks"
         class-ref="ReferencedClass"
         refresh="true"
         auto-retrieve="true"
         auto-update="true"
         auto-delete="object"
     >
         <foreignkey field-ref="fk_id"/>
<field-descriptor
         name="fk_id"
         column="fk_id"
         jdbc-type="VARCHAR"
         length="35"
     >
</class-descriptor>

<class-descriptor
     class="ReferencedClass"
     table="table2"
 >
     <field-descriptor
         name="ID"
         column="pk_id"
         jdbc-type="VARCHAR"
         primarykey="true"
         length="35"
     >
</class-descriptor>





---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: deleteByQuery does not cascade deletes

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi bradford,

delete by query is using pure sql and works 'outside' ojb.
ojb does not know which objects are deleted, that's why you have to clear the 
cache manually afterwards.

hth
jakob

Bradford Pielech wrote:

> Hello again:
> 
>     I am writing some JUnit tests now and as part of the setup, I want 
> to make sure the DB is empty, so I wrote a method to delete all relevant 
> objects and their related objects:
> 
> ----------------
> public boolean deleteAllObjects(String classNameOfObject) {
>     boolean returnValue = true;
>     Class exampleClass = Class.forName(classNameOfObject);
>     Query query = QueryFactory.newQuery(exampleClass,
>                                         
> QueryByCriteria.CRITERIA_SELECT_ALL);
>     PersistenceBroker pbroker = PersistenceBrokerFactory.
>         defaultPersistenceBroker();
>       pbroker.beginTransaction();
>       pbroker.deleteByQuery(query);
>       pbroker.commitTransaction();
> }
> -------------------
> 
> However, when I run this, only the objects from the Primary class are 
> deleted (see below) and not any associated ReferencedObjects.  This was 
> confirmed by looking at the p6spy logI changed the code to deleteAll by 
> first retrieving all objects of the type and then calling delete on each:
> 
> ----------
> public boolean deleteAllObjects(String classNameOfObject) {
>        PersistenceBroker pbroker = 
> PersistenceBrokerFactory.defaultPersistenceBroker();
>     try {
>       List objects = getObjectsOfType(classNameOfObject);      //uses 
> getCollectionByQuery and CRITERIA_SELECT_ALL
>       pbroker.beginTransaction();
>       for (Iterator i = objects.iterator(); i.hasNext(); ) {
>         pbroker.delete(i.next());
>       }
>       pbroker.deleteByQuery(query);
>       pbroker.commitTransaction();
> ------------
> 
> And this version successfully deleted all objects and their associations 
> (again confirmed by p6spy).  Now I know the mapping file is correct, 
> otherwise the 2nd version would have failed.  I included relevant 
> sections of it below, just in case.
> 
> Any ideas what I am doing wrong?
> 
> thanks,
> Brad
> 
> --------------------
> <class-descriptor
>     class="PrimaryClass"
>     table="table1"
>  >
> <reference-descriptor
>         name="tasks"
>         class-ref="ReferencedClass"
>         refresh="true"
>         auto-retrieve="true"
>         auto-update="true"
>         auto-delete="object"
>     >
>         <foreignkey field-ref="fk_id"/>
> <field-descriptor
>         name="fk_id"
>         column="fk_id"
>         jdbc-type="VARCHAR"
>         length="35"
>     >
> </class-descriptor>
> 
> <class-descriptor
>     class="ReferencedClass"
>     table="table2"
>  >
>     <field-descriptor
>         name="ID"
>         column="pk_id"
>         jdbc-type="VARCHAR"
>         primarykey="true"
>         length="35"
>     >
> </class-descriptor>
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org