Re: Deleting items in collection?

"Vasily Ivanov" <[email protected]>
Newsgroups gmane.comp.jakarta.ojb.user
Message-ID <[email protected]>
Hi Sebastian,

>if I delete an ClassB object from the collection in
>ClassA and store the ClassA object again, this
>ClassB object is not removed from the database
It depends on how you delete this object from collection. You should
get object (aObject) with all references using OJB query, first. Than
remove object (bObject) from collection (by calling
aObject.getBs().remove(bObject)) and than call broker.store(aObject).
That should remove bObject from db.

Cheers,
  Vasily

On 8/21/06, Sebastian Stein <[email protected]> wrote:
> Hi,
>
> this might be an easy question, I'm quite new to OJB. I have a question concerning the following example:
>
> Class A has a primary key field "PKEY" with type int and other elements in addition.
>
> Class B has "PKEY" as a foreign key field plus another additional field. Together, both entries are the primary key for Class B.
>
> Now, if I query Class A in source code, I want to get a Collection of objects of Class B belonging to Class A. So in repository.xml I did the following:
>
> <class-descriptor
>     class="namespace.ClassA"
>     table="table_class_a"
> >
>     <field-descriptor id="1"
>         name="pkey"
>         column="pkey"
>         jdbc-type="INTEGER"
>         primarykey="true"
>         autoincrement="true"
>     />
>     ....
>     <collection-descriptor
>         name="class_b"
>         element-class-ref="namespace.ClassB"
>         auto-retrieve="true"
>         auto-update="true"
>         auto-delete="true"
>         orderby="pkey"
>         sort="DESC"
>     >
>     <inverse-foreignkey field-id-ref="1"/>
>     </collection-descriptor>
> </class-descriptor>
>
> And here for ClassB:
>
> <class-descriptor
>     class="namespace.ClassB"
>     table="table_class_b"
> >
>     <field-descriptor id="1"
>         name="pkey"
>         column="pkey"
>         jdbc-type="INTEGER"
>         primarykey="true"
>     />
>     <field-descriptor id="2"
>         name="other"
>         column="other"
>         jdbc-type="VARCHAR"
>         primarykey="true"
>     />
> </class-descriptor>
>
> Now, if I create in my source a class A instance and add a class B collection to it, everything goes fine. Class A is stored in the DB as well as all instances of class B.
>
> On deletion of class A, I also start a query to delete all B connected to it, so something like:
>
> broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey);
> Criteria crit = new Criteria();
> crit.addEqualTo("pkey", new Integer(pkey));
> Query query_A = new QueryByCriteria(namespace.ClassA.class, crit);
> Query query_B = new QueryByCriteria(namespace.ClassB.class, crit);
> broker.deleteByQuery(queryA);
> broker.deleteByQuery(queryA);
>
> If I insert a new instance of ClassB to the collection of ClassB objects in ClassA, this new instance is also automatically added to the DB. However, and now we get to the problem, if I delete an ClassB object from the collection in ClassA and store the ClassA object again, this ClassB object is not removed from the database.
>
> What do I have to do to automatically trigger the deletion of this object?
>
>
> Thanks for your help,
>
>
> Sebastian
> --
>
>
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.