Hi,
Scott W. Hill wrote:
> I'm pretty new to OJB, and experiencing something I didn't expect. In my
> system, Articles have an associated Group (1:1), Author (1:1), Category
> (1:1). My unit test looks like this:
>
> ----------------
> public void testInsertArticle() {
> Article a = new Article();
> a.setAuthorId(1);
> a.setCategoryId(1);
> a.setGroupId(1);
> a.setName("This is a test post.");
> a.setDescription("This is a description of a test post.");
> a.setBody("This is the body of the post. It's got lots of juicy
> info.");
> a.setActive(true);
> try {
> broker.beginTransaction();
> broker.store(a);
> broker.commitTransaction();
> } catch (Throwable e) {
> e.printStackTrace();
> broker.abortTransaction();
> fail();
> } finally {
> broker.close();
> // broker.clearCache();
> a = null;
> }
> }
>
> public void testSelectArticles() {
> criteria = new Criteria();
> criteria.addEqualTo("name", "This is a test post.");
> query = new QueryByCriteria(Article.class, criteria);
> broker.beginTransaction();
> Article a = (Article) broker.getObjectByQuery(query);
> assertNotNull(a);
> User author = a.getAuthor();
> assertNotNull(author);
> Group g = a.getGroup();
> assertNotNull(g);
> broker.commitTransaction();
> broker.close();
> }
> ----------------
>
> If I don't clear the cache (notice the commented out line), the second
> test fails because (I assume) the cache is handing back the object from
> the first test,
correct !
> which doesn't have the group or author objects assigned.
> Clearing the cache works correctly. Is this the intended behavior?
This is the normal OJB behaviour. if an object is already cached use it
and don't look up the db again.
> Should I have to clear the cache after each insert to force OJB to fetch
> the object again?
>
It's not necessary to empty the whole cache.
1. you can remove single instances from cache by
broker.removeFromCache(instance).
2. you can tell ojb to refresh its references from the db even if it is
found in the cache. See documentation on reference-descriptor and
collection-descriptor in repository.html (attribute refresh="true").
cheers,
Thomas
> --Scott
>
>
> --
> To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>