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 Sebastian Stein <se...@gmx.de> on 2006/08/21 11:04:17 UTC
Deleting items in collection?
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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Deleting items in collection?
Posted by Sebastian Stein <se...@gmx.de>.
Edson Carlos Ericksson Richter <ed...@mgrinformatica.com.br> [060824
21:44]:
> I've been using RemovalAwareList in the descriptor, so I don't need to
> take care of this in coding.
I was able to fix it with the RemovalAwareList in the java code. I think one
important point was that I called twice the store functionality, so:
aObject.getBs().clear()
aObject.store();
aObject.getAllBs();
aObject.store();
Sebastian
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Deleting items in collection?
Posted by Edson Carlos Ericksson Richter <ed...@mgrinformatica.com.br>.
I've been using RemovalAwareList in the descriptor, so I don't need to
take care of this in coding.
Regards,
Richter
Carlos Chávez escreveu:
> Sebastian Stein escribió:
>> Vasily Ivanov <ba...@gmail.com> [060821 23:46]:
>>
>>>> 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.
>>>
>>
>> I really don't understand why it is not working. I do the following:
>>
>> aObject.getBs().clear();
>> aObject.getBs().addAll(newBs);
>>
>> But the old Bs are not removed, just the new Bs are added. This is
>> the case
>> if I have auto-delete="false" in the repository.xml for the
>> definition of
>> the objectB collection.
>>
>> If I turn it on, it deletes all Bs without adding them again. It is very
>> strange.
>>
>>
> Hi Sebastian.
>
> We had some similar situation, we solved in this way:
>
> OBJ have a RemovalAwareList class, so i create a List usign
> RemovalAwareList(), copy the childs to this
> list, then remove the object from the list then i commit the changes.
>
> Example:
> List lst = new RemovalAwareList();
> lst.addAll(object1.getChilds());
> lst.remove(child);
> object1.setChilds(lst); So when you commit your
> changes OJB delete the object.
>
> I hope this help.
>
> Cheers,
> Carlos Chávez.
>> Sebastian
>>
>> PS: If you are interested I can also point you directly to the code,
>> it is
>> public.
>>
>> ---------------------------------------------------------------------
>> 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
>
>
>
>
Re: Deleting items in collection?
Posted by Carlos Chávez <cc...@agssa.net>.
Sebastian Stein escribió:
> Vasily Ivanov <ba...@gmail.com> [060821 23:46]:
>
>>> 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.
>>
>
> I really don't understand why it is not working. I do the following:
>
> aObject.getBs().clear();
> aObject.getBs().addAll(newBs);
>
> But the old Bs are not removed, just the new Bs are added. This is the case
> if I have auto-delete="false" in the repository.xml for the definition of
> the objectB collection.
>
> If I turn it on, it deletes all Bs without adding them again. It is very
> strange.
>
>
Hi Sebastian.
We had some similar situation, we solved in this way:
OBJ have a RemovalAwareList class, so i create a List usign
RemovalAwareList(), copy the childs to this
list, then remove the object from the list then i commit the changes.
Example:
List lst = new RemovalAwareList();
lst.addAll(object1.getChilds());
lst.remove(child);
object1.setChilds(lst);
So when you commit your changes OJB delete the object.
I hope this help.
Cheers,
Carlos Chávez.
> Sebastian
>
> PS: If you are interested I can also point you directly to the code, it is
> public.
>
> ---------------------------------------------------------------------
> 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
Re: Deleting items in collection?
Posted by Sebastian Stein <se...@gmx.de>.
Vasily Ivanov <ba...@gmail.com> [060821 23:46]:
> >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.
I really don't understand why it is not working. I do the following:
aObject.getBs().clear();
aObject.getBs().addAll(newBs);
But the old Bs are not removed, just the new Bs are added. This is the case
if I have auto-delete="false" in the repository.xml for the definition of
the objectB collection.
If I turn it on, it deletes all Bs without adding them again. It is very
strange.
Sebastian
PS: If you are interested I can also point you directly to the code, it is
public.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Deleting items in collection?
Posted by Sebastian Stein <se...@gmx.de>.
Vasily Ivanov <ba...@gmail.com> [060821 23:46]:
> >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.
I really don't understand why it is not working. I do the following:
aObject.getBs().clear();
aObject.getBs().addAll(newBs);
But the old Bs are not removed, just the new Bs are added. This is the case
if I have auto-delete="false" in the repository.xml for the definition of
the objectB collection.
If I turn it on, it deletes all Bs without adding them again. It is very
strange.
Sebastian
PS: If you are interested I can also point you directly to the code, it is
public.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Deleting items in collection?
Posted by Vasily Ivanov <ba...@gmail.com>.
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 <se...@gmx.de> 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: 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