On Mon, May 17, 2010 at 2:54 AM, Anton Podviaznikov
<[email protected]> wrote:

> 1. I created sample application. We have company domain model. I want to add
> company and view the list of companies.
> I have following code:
>         public void addCompany(String name)
>         {
>             final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
>             try
>             {
>                 final EntityBuilder<Company> builder =
> uow.newEntityBuilder(Company.class);
>                 final Company prototype = builder.instance();
>                 prototype.name().set(name);
>                 final Company company = builder.instance();
>
>                 System.out.println(company.name());
>             }
>             finally
>             {
>                 try
>                 {
>                     if(uow!=null)uow.complete();
>                 }
>                 catch (UnitOfWorkCompletionException e)
>                 {
>                     debug.debug(Debug.HIGH, "Exception during UOW complete.
> See: " + e.getMessage());
>                 }
>             }
>         }
> Is it enough to store entity or I missed something?

Typically,
1. You put the newUnitOfWork call at the "request boundary", such as
the entry method of a servlet request.

2. All the domain model code use currentUnitOfWork().

3. The try catch is typically;

UnitOfWork uow = uowf.newUnitOfWork();
try
{
    model.someMethod();
    uow.complete();
}
catch( Exception e )
{
    uow.discard();
}

You could have a more elaborate one that perhaps support "retry" if
there is a ConcurrentEntityModificationException.


> Because I have next method:
>         public Query<Company> findAll()
>         {
>             final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
>             try
>             {
>                 return qbf.newQueryBuilder(Company.class).newQuery(uow);
>             }
>             finally
>             {
>                 try
>                 {
>                     if(uow!=null)uow.complete();
>                 }
>                 catch (UnitOfWorkCompletionException e)
>                 {
>                     debug.debug(Debug.HIGH, "Exception during UOW complete.
> See: " + e.getMessage());
>                 }
>             }
>         }
>
> and it retus empty list.

Stanislav pointed out that you need uow.complete() before the indexing
picks up the changes. But if this is the actual code that should still
be the case. Question is how the Indexing subsystem is assembled.

> 2. Another question regarding UnitOfWork. DO I use tham correctly? What is
> the pattern, when I should create new or use current?

Model code should use currentUnitOfWork(), since you probably want to
be agnostic of modification boundaries in your model. We also have
annotation support for UnitOfWork, if you are lazy.

See @UnitOfWorkPropagation/UnitOfWorkConcern... but keep in mind it is
an early and largely undocumented implementation which may change in
the future.


Cheers
-- 
Niclas Hedhman, Software Developer
http://www.qi4j.org - New Energy for Java

I  live here; http://tinyurl.com/2qq9er
I  work here; http://tinyurl.com/2ymelc
I relax here; http://tinyurl.com/2cgsug

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to