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 Olaf Meske <ol...@iqnex.com> on 2003/03/07 21:27:10 UTC

question for small example about deleting in n:m mapping with decomposition tables

Hi,

I'm just starting with ojb and odmg and I'v got a question for a small 
example, showing the handling with an n:m mappings like the 
person-role-project example in the tutorial with odmg and automatic 
*delete* of roles (also inside the db table), e.g. when calling 
person.getAllRoles().remove(someRole);

I tried it like in this example:
[...]
tx = odmg.newTransaction();
tx.begin();
// 3. lookup the person specified by query
String oqlQuery = "select Person from " + Person.class.getName()
     +" where id =" + id;
OQLQuery query = odmg.newOQLQuery();
query.create(oqlQuery);
DList result = (DList) query.execute();
Person toBeEdited = (Person) result.get(0);
tx.lock(toBeEdited, Transaction.WRITE);

toBeEdited.getRoles().remove
for (Iterator iter = toBeEdited.getRoles().iterator(); iter.hasNext();) {
     iter.next();
     iter.remove();
}
tx.commit();

this should remove all roles from person (also inside the db) and not
just inside the cache.

[...]

All I found in the mail archive are some hints about:
- DListImpl, but than my relations must be stored inside the ojb_dlist
   table?! Together with
   collection-class="org.apache.ojb.odmg.collections.DListImpl"
- Don't use auto-update and auto-delete with odmg inside my repository
- A mail that dlist deletions just goes to goes to ojb_dlist
http://archives.apache.org/eyebrowse/ReadMsg?listName=ojb-user@jakarta.apache.org&msgId=421122
   and that this behaviour is right, because odmg don't know about other
   references (but this is just a composition table, and I want to delete
   these entries).

All I need is a simple example or a definitiv answer how to handle this.

Olaf



Re: question for small example about deleting in n:m mapping with decomposition tables

Posted by Olaf Meske <ol...@iqnex.com>.
Many thanks!

That should work for. But I thought there would be an attribute for the 
repository that do this job. Something like delete all cascading objects
for plain composition entities until here.

<class-descriptor
   class="Person"
   table="PERSON"
 >
[...]
   <collection-descriptor
     name="roles"
     element-class-ref="Role"
     *auto-delete-ref="true"* <-- That deletes the referenced composition
                                  entity
    >
     <inverse-foreignkey field-ref="person_id"/>
   </collection-descriptor>
</class-descriptor>

> Here is code that will hopefully do the job (assuming you use a simple 
> Vector):
> 
>  Person toBeEdited = (Person) result.get(0);
> 
>  for (Iterator iter = toBeEdited.getRoles().iterator(); iter.hasNext();)
>  {
>      db.deletePersistent(iter.next()); // The database instance you
>                                        // opened somewhere before
>      iter.remove();
>  }
>  tx.commit();

Olaf



Re: question for small example about deleting in n:m mapping with decomposition tables

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

Olaf Meske wrote:
> Hi,
> 
> I'm just starting with ojb and odmg and I'v got a question for a small 
> example, showing the handling with an n:m mappings like the 
> person-role-project example in the tutorial with odmg and automatic 
> *delete* of roles (also inside the db table), e.g. when calling 
> person.getAllRoles().remove(someRole);
> 
> I tried it like in this example:
> [...]
> tx = odmg.newTransaction();
> tx.begin();
> // 3. lookup the person specified by query
> String oqlQuery = "select Person from " + Person.class.getName()
>     +" where id =" + id;
> OQLQuery query = odmg.newOQLQuery();
> query.create(oqlQuery);
> DList result = (DList) query.execute();
> Person toBeEdited = (Person) result.get(0);
> tx.lock(toBeEdited, Transaction.WRITE);
> 
> toBeEdited.getRoles().remove
> for (Iterator iter = toBeEdited.getRoles().iterator(); iter.hasNext();) {
>     iter.next();
>     iter.remove();
> }
> tx.commit();

The semantics of this code depends on the collection class you choose 
for the Person.getRoles() collection in repository.xml.
1. If you do not specify a collection a normal vector is used.
For a normal vector Vector.remove(...) does not inform the ODMG 
transaction about the deletion! So on tx commit nothing happens.

2. If you use 
collection-class="org.apache.ojb.odmg.collections.DListImpl" things look 
different: DList.remove(...) interacts with the ODMG transaction and on 
tx commit the entries are removed from the Dlist.
Well, the entries (that is references to the actual role objects) but 
not the role instances themselves.

Both options won't make you happy.
Here is code that will hopefully do the job (assuming you use a simple 
Vector):

  Person toBeEdited = (Person) result.get(0);

  for (Iterator iter = toBeEdited.getRoles().iterator(); iter.hasNext();)
  {
      db.deletePersistent(iter.next()); // The database instance you
				       // opened somewhere before
      iter.remove();
  }
  tx.commit();

cheers,
Thomas

> 
> this should remove all roles from person (also inside the db) and not
> just inside the cache.
> 
> [...]
> 
> All I found in the mail archive are some hints about:
> - DListImpl, but than my relations must be stored inside the ojb_dlist
>   table?! Together with
>   collection-class="org.apache.ojb.odmg.collections.DListImpl"
> - Don't use auto-update and auto-delete with odmg inside my repository
> - A mail that dlist deletions just goes to goes to ojb_dlist
> http://archives.apache.org/eyebrowse/ReadMsg?listName=ojb-user@jakarta.apache.org&msgId=421122 
> 
>   and that this behaviour is right, because odmg don't know about other
>   references (but this is just a composition table, and I want to delete
>   these entries).
> 
> All I need is a simple example or a definitiv answer how to handle this.
> 
> Olaf
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
>