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 "McKinstry, Pete (HQP)" <Pe...@rhi.com> on 2003/03/18 00:32:39 UTC

auto-delete=true/ cascading delete.

Does anyone have an example of a 1 to many relationship using a cascading
delete? I checked the unit tests briefly, but couldn't find any tests of the
cascading options. (Did i miss it?) 

I expected the auto-delete="true" to delete the child records along w/ the
parent, but i couldn't get it to work. I'm wondering what i'm doing wrong.
(i don't have any cascading delete options set in the database. is this
required?) 

My mapping file looks like this:
 <class-descriptor class="com.rhi.reports.common.bean.ReportRequest" 
			table="REPORT_REQUEST" schema="PETMCK01">

      <field-descriptor 
         name="requestId" 
         column="REPORT_REQUEST_ID" 
         jdbc-type="BIGINT"
         primarykey="true" 
         autoincrement="true" />
         ...
      <collection-descriptor
         name="statuses"
         element-class-ref="com.rhi.reports.common.bean.ReportRequestStatus"
         orderby="statusId"
         sort="DESC"
         auto-retrieve="true"
         auto-update="true"
         auto-delete="true"
      >
         <inverse-foreignkey field-ref="reportRequestId"/>
      </collection-descriptor>

 </class-descriptor>

My code looks like this:

        Criteria crit = new Criteria();
        crit.addEqualTo("userId", "testuser");

        Query query = new QueryByCriteria(ReportRequest.class, crit);
        ReportRequest request =
(ReportRequest)broker.getObjectByQuery(query);
        
        broker.beginTransaction();
        broker.delete(request);
        broker.commitTransaction();

The row in the status table stays, while the referenced request is gone. 

Any ideas?

-pgm

Re: auto-delete=true/ cascading delete.

Posted by Thomas Mahler <th...@web.de>.
Hi Pete,

McKinstry, Pete (HQP) wrote:
> Does anyone have an example of a 1 to many relationship using a cascading
> delete? I checked the unit tests briefly, but couldn't find any tests of the
> cascading options. (Did i miss it?) 

There are several Testcases covering this feature. In 
repository_junit.xml search for auto-delete="true". This will guide you 
to entity classes used in those testcases.

> I expected the auto-delete="true" to delete the child records along w/ the
> parent, 

correct!

> but i couldn't get it to work. 

:-(

> I'm wondering what i'm doing wrong.
> (i don't have any cascading delete options set in the database. is this
> required?) 

No the OJB cascading operation does not rely on database settings.

> 
> My mapping file looks like this:
>  <class-descriptor class="com.rhi.reports.common.bean.ReportRequest" 
> 			table="REPORT_REQUEST" schema="PETMCK01">
> 
>       <field-descriptor 
>          name="requestId" 
>          column="REPORT_REQUEST_ID" 
>          jdbc-type="BIGINT"
>          primarykey="true" 
>          autoincrement="true" />
>          ...
>       <collection-descriptor
>          name="statuses"
>          element-class-ref="com.rhi.reports.common.bean.ReportRequestStatus"
>          orderby="statusId"
>          sort="DESC"
>          auto-retrieve="true"
>          auto-update="true"
>          auto-delete="true"
>       >
>          <inverse-foreignkey field-ref="reportRequestId"/>
>       </collection-descriptor>
> 
>  </class-descriptor>

looks OK!

> My code looks like this:
> 
>         Criteria crit = new Criteria();
>         crit.addEqualTo("userId", "testuser");
> 
>         Query query = new QueryByCriteria(ReportRequest.class, crit);
>         ReportRequest request =
> (ReportRequest)broker.getObjectByQuery(query);

are you sure that the request.statuses collection is filled at this 
point? If not no sttus entries will get deleted!


>         broker.beginTransaction();
>         broker.delete(request);
>         broker.commitTransaction();
> 
> The row in the status table stays, while the referenced request is gone. 
> 
> Any ideas?

The cascade delete feature worked without problems for the last two 
years. So I think that there may be a problem with your mapping. How 
does the class-descriptor for the ReportRequestStatus class look like?

cheers, Thomas

> -pgm
>