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 Sean Dockery <sd...@securac.net> on 2004/03/31 18:25:57 UTC

What should I use for the initial value of a collection field in a new object? and a related question.

I have an application that uses the ODMG personality of OJB.  I query 
objects and collections using PersistenceBroker Criteria; the 
PersistenceBroker is obtained from a Transaction object (via 
TransactionExt).  Objects are stored via a tx.lock plus a tx.markDirty 
call.  The application uses the default cache implementation.

My question is:  What should I use as the initial value of a collection 
field in newly created business objects?

Consider the following simple class:

public class Category {

	private Collection products;

	public Collection getProducts() {
		return products;
	}

	public void setProducts(Collection newProducts) {
		products = newProducts;
	}

}

When I create the Category object and store it in the cache, the next 
fetch retrieves that same Category object reference that was stored. 
The products field is null because it was never initialized.

What value should I use when initializing the field?  Can I use a plain 
old Collection of some sort (LinkedList, ArrayList, HashMap, et al) or 
are there some factory methods inside OJB from which I can obtain an 
instance of the default collection class.

One other thing:  Because collection fields of objects retrieved from 
the database are typically of type RemovalAwareCollection, is it safe to 
replace the field value with a setProducts(plainOldList) or should I be 
modifying the collection via getProducts().add() and 
getProducts().remove()?  Does it matter?


These two questions seem related (to me) by the fact that if I use a 
standard Collection subclass when I create an object and store it in the 
cache, it will never see a RemovalAwareCollection (for example) until 
the object has been purged from the cache and reloaded.


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


Re: What should I use for the initial value of a collection field in a new object? and a related question.

Posted by Sean Dockery <sd...@securac.net>.
Armin Waibel wrote:
> Hi Sean,
> 
> Sean Dockery wrote:
> 
>> Still no recommendations?  (I'm posting this through Gmane; can 
>> everyone on the mailing list read this?)
>>
> 
> Sorry for the late reply, but I'm overwhelmed with posts and todo's.

No worries.  I was just beginning to wonder if my posts were going 
beyond Gmane.

> 
>> Sean Dockery wrote:
>>
>>> No recommendations?
>>>
>>> Sean Dockery wrote:
>>>
>>>> I have an application that uses the ODMG personality of OJB.  I 
>>>> query objects and collections using PersistenceBroker Criteria; the 
>>>> PersistenceBroker is obtained from a Transaction object (via 
>>>> TransactionExt).  Objects are stored via a tx.lock plus a 
>>>> tx.markDirty call.  The application uses the default cache 
>>>> implementation.
>>>>
>>>> My question is:  What should I use as the initial value of a 
>>>> collection field in newly created business objects?
>>>>
>>>> Consider the following simple class:
>>>>
>>>> public class Category {
>>>>
>>>>     private Collection products;
>>>>
>>>>     public Collection getProducts() {
>>>>         return products;
>>>>     }
>>>>
>>>>     public void setProducts(Collection newProducts) {
>>>>         products = newProducts;
>>>>     }
>>>>
>>>> }
>>>>
>>>> When I create the Category object and store it in the cache, the 
>>>> next fetch retrieves that same Category object reference that was 
>>>> stored. The products field is null because it was never initialized.
>>>>
> 
> What do you mean with 'create' - query for an Category or insert a new 
> Category object?

I'm talking about the case of inserting a new Category object that does 
not exist in the database.

> Did you set auto-retrieve 'false' in your collection-descriptor for 
> product? In that case call PB.retrieveAllReferences(...) on the main 
> object to force loading of references (and cache object again if you
> want the full materialized object cached)

I typically have auto-retrieve set to true.

> You can use the 'refresh' attribute in the collection-descriptor to 
> force loading of the collection on each main object request (this work 
> for cached objects too) - need auto-retrieve 'true'
> 

I have seen the refresh attribute in the DTD, but I have been unsure as 
to how it works.  I will try this.

>>>> What value should I use when initializing the field?  Can I use a 
>>>> plain old Collection of some sort (LinkedList, ArrayList, HashMap, 
>>>> et al) or are there some factory methods inside OJB from which I can 
>>>> obtain an instance of the default collection class.
>>>>
> 
> You mean initialization on insert of main object? This will work with 
> all Collection or ManageableCollection implementations.
> 

This is useful to know.  I would prefer to create a Category object and 
assign Products to it within a single transaction.

>>>> One other thing:  Because collection fields of objects retrieved 
>>>> from the database are typically of type RemovalAwareCollection, is 
>>>> it safe to replace the field value with a setProducts(plainOldList) 
>>>> or should I be modifying the collection via getProducts().add() and 
>>>> getProducts().remove()?  Does it matter?
> 
> 
> AFAIK RemovalAwareCollection was introduced to allow collection 
> handling, in particular removal of collection objects on PB-level, in 
> the same way as on odmg-level. So I would recommend the second 
> 'modifying' way to go.
> 

I will confirm for myself that...

	tx.lock(category, Transaction.WRITE);
	category.getProducts().add(thisProduct);
	category.getProducts().add(thatProduct);
	tx.commit();

...work similarly to...

	tx.lock(category, Transaction.WRITE);
	category.setProducts(theseProducts);
	tx.commit();

>>>>
>>>>
>>>> These two questions seem related (to me) by the fact that if I use a 
>>>> standard Collection subclass when I create an object and store it in 
>>>> the cache, it will never see a RemovalAwareCollection (for example) 
>>>> until the object has been purged from the cache and reloaded.
>>
>>
> 
> hmm, valid argument never think about this. In that case you can use 
> PB.retrieveAllReferences(...)/retrieveReference(...) to re-assign the 
> collection after insert or remove the object from cache after insert.
> 

So, the following will work...

	Category category = new Category();
	tx.lock(category, Transaction.WRITE);
	Collection products = new LinkedList();
	products.add(thisProduct);
	products.add(thatProduct);
	category.setProducts(products);
	tx.commit();

	broker.retrieveReference(category, "products");

	// Confirm that our LinkedList has been replaced
	// by a ManageableCollection

	if (category.getProducts() instanceof ManageableCollection) {
		System.out.println("yes");
	}
	else {
		System.out.println("nope");
	}

...when "refresh" is set to true on the collection descriptor?

> regards,
> Armin
> 

Thanks for your time, Armin.


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


Re: What should I use for the initial value of a collection field in a new object? and a related question.

Posted by Armin Waibel <ar...@apache.org>.
Hi Sean,

Sean Dockery wrote:

> Still no recommendations?  (I'm posting this through Gmane; can everyone 
> on the mailing list read this?)
> 

Sorry for the late reply, but I'm overwhelmed with posts and todo's.

> Sean Dockery wrote:
> 
>> No recommendations?
>>
>> Sean Dockery wrote:
>>
>>> I have an application that uses the ODMG personality of OJB.  I query 
>>> objects and collections using PersistenceBroker Criteria; the 
>>> PersistenceBroker is obtained from a Transaction object (via 
>>> TransactionExt).  Objects are stored via a tx.lock plus a 
>>> tx.markDirty call.  The application uses the default cache 
>>> implementation.
>>>
>>> My question is:  What should I use as the initial value of a 
>>> collection field in newly created business objects?
>>>
>>> Consider the following simple class:
>>>
>>> public class Category {
>>>
>>>     private Collection products;
>>>
>>>     public Collection getProducts() {
>>>         return products;
>>>     }
>>>
>>>     public void setProducts(Collection newProducts) {
>>>         products = newProducts;
>>>     }
>>>
>>> }
>>>
>>> When I create the Category object and store it in the cache, the next 
>>> fetch retrieves that same Category object reference that was stored. 
>>> The products field is null because it was never initialized.
>>>

What do you mean with 'create' - query for an Category or insert a new 
Category object?

Did you set auto-retrieve 'false' in your collection-descriptor for 
product? In that case call PB.retrieveAllReferences(...) on the main 
object to force loading of references (and cache object again if you
want the full materialized object cached)


You can use the 'refresh' attribute in the collection-descriptor to 
force loading of the collection on each main object request (this work 
for cached objects too) - need auto-retrieve 'true'

>>> What value should I use when initializing the field?  Can I use a 
>>> plain old Collection of some sort (LinkedList, ArrayList, HashMap, et 
>>> al) or are there some factory methods inside OJB from which I can 
>>> obtain an instance of the default collection class.
>>>

You mean initialization on insert of main object? This will work with 
all Collection or ManageableCollection implementations.

>>> One other thing:  Because collection fields of objects retrieved from 
>>> the database are typically of type RemovalAwareCollection, is it safe 
>>> to replace the field value with a setProducts(plainOldList) or should 
>>> I be modifying the collection via getProducts().add() and 
>>> getProducts().remove()?  Does it matter?

AFAIK RemovalAwareCollection was introduced to allow collection 
handling, in particular removal of collection objects on PB-level, in 
the same way as on odmg-level. So I would recommend the second 
'modifying' way to go.

>>>
>>>
>>> These two questions seem related (to me) by the fact that if I use a 
>>> standard Collection subclass when I create an object and store it in 
>>> the cache, it will never see a RemovalAwareCollection (for example) 
>>> until the object has been purged from the cache and reloaded.
>

hmm, valid argument never think about this. In that case you can use 
PB.retrieveAllReferences(...)/retrieveReference(...) to re-assign the 
collection after insert or remove the object from cache after insert.

regards,
Armin

> 
> 
> ---------------------------------------------------------------------
> 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: What should I use for the initial value of a collection field in a new object? and a related question.

Posted by Sean Dockery <sd...@securac.net>.
Still no recommendations?  (I'm posting this through Gmane; can everyone 
on the mailing list read this?)

Sean Dockery wrote:
> No recommendations?
> 
> Sean Dockery wrote:
> 
>> I have an application that uses the ODMG personality of OJB.  I query 
>> objects and collections using PersistenceBroker Criteria; the 
>> PersistenceBroker is obtained from a Transaction object (via 
>> TransactionExt).  Objects are stored via a tx.lock plus a tx.markDirty 
>> call.  The application uses the default cache implementation.
>>
>> My question is:  What should I use as the initial value of a 
>> collection field in newly created business objects?
>>
>> Consider the following simple class:
>>
>> public class Category {
>>
>>     private Collection products;
>>
>>     public Collection getProducts() {
>>         return products;
>>     }
>>
>>     public void setProducts(Collection newProducts) {
>>         products = newProducts;
>>     }
>>
>> }
>>
>> When I create the Category object and store it in the cache, the next 
>> fetch retrieves that same Category object reference that was stored. 
>> The products field is null because it was never initialized.
>>
>> What value should I use when initializing the field?  Can I use a 
>> plain old Collection of some sort (LinkedList, ArrayList, HashMap, et 
>> al) or are there some factory methods inside OJB from which I can 
>> obtain an instance of the default collection class.
>>
>> One other thing:  Because collection fields of objects retrieved from 
>> the database are typically of type RemovalAwareCollection, is it safe 
>> to replace the field value with a setProducts(plainOldList) or should 
>> I be modifying the collection via getProducts().add() and 
>> getProducts().remove()?  Does it matter?
>>
>>
>> These two questions seem related (to me) by the fact that if I use a 
>> standard Collection subclass when I create an object and store it in 
>> the cache, it will never see a RemovalAwareCollection (for example) 
>> until the object has been purged from the cache and reloaded.


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


Re: What should I use for the initial value of a collection field in a new object? and a related question.

Posted by Sean Dockery <sd...@securac.net>.
No recommendations?

Sean Dockery wrote:
> I have an application that uses the ODMG personality of OJB.  I query 
> objects and collections using PersistenceBroker Criteria; the 
> PersistenceBroker is obtained from a Transaction object (via 
> TransactionExt).  Objects are stored via a tx.lock plus a tx.markDirty 
> call.  The application uses the default cache implementation.
> 
> My question is:  What should I use as the initial value of a collection 
> field in newly created business objects?
> 
> Consider the following simple class:
> 
> public class Category {
> 
>     private Collection products;
> 
>     public Collection getProducts() {
>         return products;
>     }
> 
>     public void setProducts(Collection newProducts) {
>         products = newProducts;
>     }
> 
> }
> 
> When I create the Category object and store it in the cache, the next 
> fetch retrieves that same Category object reference that was stored. The 
> products field is null because it was never initialized.
> 
> What value should I use when initializing the field?  Can I use a plain 
> old Collection of some sort (LinkedList, ArrayList, HashMap, et al) or 
> are there some factory methods inside OJB from which I can obtain an 
> instance of the default collection class.
> 
> One other thing:  Because collection fields of objects retrieved from 
> the database are typically of type RemovalAwareCollection, is it safe to 
> replace the field value with a setProducts(plainOldList) or should I be 
> modifying the collection via getProducts().add() and 
> getProducts().remove()?  Does it matter?
> 
> 
> These two questions seem related (to me) by the fact that if I use a 
> standard Collection subclass when I create an object and store it in the 
> cache, it will never see a RemovalAwareCollection (for example) until 
> the object has been purged from the cache and reloaded.


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