You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Luther Baker <lu...@gmail.com> on 2008/12/05 19:20:06 UTC

architectural question

If I inject a Hibernate session into my DAO and have my Tapestry Controller
invoke that, is there a Tapestry way that I can tell hibernate to start and
commit a transaction via annotations or configuration?

IE:
http://tapestry.apache.org/tapestry5/tapestry-core/ref/org/apache/tapestry5/corelib/components/BeanEditForm.html

public class CreateUser
{
    @Inject
    private UserDAO userDAO;

...
    Object onSuccess()
    {
        userDAO.add(user);

        return UserAdmin.class;
    }
}

public class UserDAO
{
    @Inject
    private Session session;

    void add(User user)
    {
        session.persist(user);
    }
}


I tried to use @CommitAfter in my DAO but that didn't work. I essentially
want to simulate:

    void add(User user)
    {
        session.beginTransaction();
        session.persist(user);
        sessino.getTransaction().commit();
    }

but I don't want to explicitly code this in my DAO for fear that I may have
some instances where the the transactional boundaries must be larger.

Maybe this isn't a Tapestry specific thing - but I'm not sure how to inject
the Hibernate Session into my DAO but have my Tapestry Controller define the
transactional boundaries ... was hoping some Tapestry annotations might help
me out.

-Luther

Re: architectural question

Posted by Luther Baker <lu...@gmail.com>.
That works just fine when I @Inject a hibernate session directly into a DAO
and say, persist an object ... but there's a catch.

In my world - a DAO is a very low level, granular thing. I inject DAOs into
my PersistenceService which gets invoked from my Management layer and I want
the Transaction definitions at the *PersistenceService *layer - not the
individual DAO layer. If I need to atomically commit 5 saves - I need to
define a service layer method with @CommitAfter.

In the following code snippet, I have the @CommitAfter where I'd logically
like it ... unfortunately, nothing gets persisted when I put it there. I
know the path is correct (web IS submitting a valid project object since,
this works fine when @CommitAfter is on the DAO method and the AppModule
@Match tag changes accordingly.

Thoughts?


*Tapestry config:
*
public class *AppModule*
{
    public static void bind(ServiceBinder binder)
    {
        binder.bind(ProjectManager.class, SimpleProjectManager.class);
        binder.bind(WriteDao.class, HibernateWriteDao.class);
        binder.bind(PersistenceService.class, PersistenceService.class);
    }

    @Match("*PersistenceService")
    public static <T> T
decorateTransactionally(HibernateTransactionDecorator decorator,
                                                Class<T> serviceInterface,
                                                T delegate,
                                                String serviceId)
    {
        T obj = decorator.build(serviceInterface, delegate, serviceId);
        return obj;
    }
}

*from the web request:*

public class *CreateProject*
{
    @Inject
    private ProjectManager projectManager;

    @Property
    private Project newProject;

    Object onSuccess()
    {
        projectManager.createProject(newProject);
        return ListProjects.class;
    }
}

*invoked from CreateProject.onSuccess*:

public class SimpleProjectManager implements ProjectManager
{
    @Inject
    private PersistenceService persistenceService;

    public void createProject(Project project)
    {
        persistenceService.createProject(project);
    }
}

*invoked from SimpleProjectManager:**

* public class PersistenceService
{
    @Inject
    private Session session;

    @Inject
    private WriteDao writeDao;

    @CommitAfter
    public void createProject(Project project)
    {
        // imagine other db saves here - and assume these must all
succeed... or all fail
        writeDao.persist(session, project);
    }

}

*and finally
*
public class HibernateWriteDao implements WriteDao
{
    public void persist(Session session, Object obj)
    {
        session.persist(obj);
    }
}



On Fri, Dec 5, 2008 at 6:04 PM, Luther Baker <lu...@gmail.com> wrote:

> Thanks - that worked just fine.
>
> -Luther
>
>
>
> On Fri, Dec 5, 2008 at 1:41 PM, James Hillyerd <ja...@hillyerd.com> wrote:
>
>> Look at the bottom of this page:
>>
>> http://tapestry.apache.org/tapestry5/tapestry-hibernate/userguide.html
>>
>> It explains how to get @CommitAfter working with DAOs.
>>
>> -james
>>
>> On Fri, Dec 5, 2008 at 10:20 AM, Luther Baker <lu...@gmail.com>
>> wrote:
>>
>> > If I inject a Hibernate session into my DAO and have my Tapestry
>> Controller
>> > invoke that, is there a Tapestry way that I can tell hibernate to start
>> and
>> > commit a transaction via annotations or configuration?
>> >
>> > IE:
>> >
>> >
>> http://tapestry.apache.org/tapestry5/tapestry-core/ref/org/apache/tapestry5/corelib/components/BeanEditForm.html
>> >
>> > public class CreateUser
>> > {
>> >    @Inject
>> >    private UserDAO userDAO;
>> >
>> > ...
>> >    Object onSuccess()
>> >    {
>> >        userDAO.add(user);
>> >
>> >        return UserAdmin.class;
>> >    }
>> > }
>> >
>> > public class UserDAO
>> > {
>> >    @Inject
>> >    private Session session;
>> >
>> >    void add(User user)
>> >    {
>> >        session.persist(user);
>> >    }
>> > }
>> >
>> >
>> > I tried to use @CommitAfter in my DAO but that didn't work. I
>> essentially
>> > want to simulate:
>> >
>> >    void add(User user)
>> >    {
>> >        session.beginTransaction();
>> >        session.persist(user);
>> >        sessino.getTransaction().commit();
>> >    }
>> >
>> > but I don't want to explicitly code this in my DAO for fear that I may
>> have
>> > some instances where the the transactional boundaries must be larger.
>> >
>> > Maybe this isn't a Tapestry specific thing - but I'm not sure how to
>> inject
>> > the Hibernate Session into my DAO but have my Tapestry Controller define
>> > the
>> > transactional boundaries ... was hoping some Tapestry annotations might
>> > help
>> > me out.
>> >
>> > -Luther
>> >
>>
>>
>>
>> --
>> James A. Hillyerd <ja...@hillyerd.com>
>>
>
>

Re: architectural question

Posted by Luther Baker <lu...@gmail.com>.
Thanks - that worked just fine.

-Luther


On Fri, Dec 5, 2008 at 1:41 PM, James Hillyerd <ja...@hillyerd.com> wrote:

> Look at the bottom of this page:
>
> http://tapestry.apache.org/tapestry5/tapestry-hibernate/userguide.html
>
> It explains how to get @CommitAfter working with DAOs.
>
> -james
>
> On Fri, Dec 5, 2008 at 10:20 AM, Luther Baker <lu...@gmail.com>
> wrote:
>
> > If I inject a Hibernate session into my DAO and have my Tapestry
> Controller
> > invoke that, is there a Tapestry way that I can tell hibernate to start
> and
> > commit a transaction via annotations or configuration?
> >
> > IE:
> >
> >
> http://tapestry.apache.org/tapestry5/tapestry-core/ref/org/apache/tapestry5/corelib/components/BeanEditForm.html
> >
> > public class CreateUser
> > {
> >    @Inject
> >    private UserDAO userDAO;
> >
> > ...
> >    Object onSuccess()
> >    {
> >        userDAO.add(user);
> >
> >        return UserAdmin.class;
> >    }
> > }
> >
> > public class UserDAO
> > {
> >    @Inject
> >    private Session session;
> >
> >    void add(User user)
> >    {
> >        session.persist(user);
> >    }
> > }
> >
> >
> > I tried to use @CommitAfter in my DAO but that didn't work. I essentially
> > want to simulate:
> >
> >    void add(User user)
> >    {
> >        session.beginTransaction();
> >        session.persist(user);
> >        sessino.getTransaction().commit();
> >    }
> >
> > but I don't want to explicitly code this in my DAO for fear that I may
> have
> > some instances where the the transactional boundaries must be larger.
> >
> > Maybe this isn't a Tapestry specific thing - but I'm not sure how to
> inject
> > the Hibernate Session into my DAO but have my Tapestry Controller define
> > the
> > transactional boundaries ... was hoping some Tapestry annotations might
> > help
> > me out.
> >
> > -Luther
> >
>
>
>
> --
> James A. Hillyerd <ja...@hillyerd.com>
>

Re: architectural question

Posted by James Hillyerd <ja...@hillyerd.com>.
Look at the bottom of this page:

http://tapestry.apache.org/tapestry5/tapestry-hibernate/userguide.html

It explains how to get @CommitAfter working with DAOs.

-james

On Fri, Dec 5, 2008 at 10:20 AM, Luther Baker <lu...@gmail.com> wrote:

> If I inject a Hibernate session into my DAO and have my Tapestry Controller
> invoke that, is there a Tapestry way that I can tell hibernate to start and
> commit a transaction via annotations or configuration?
>
> IE:
>
> http://tapestry.apache.org/tapestry5/tapestry-core/ref/org/apache/tapestry5/corelib/components/BeanEditForm.html
>
> public class CreateUser
> {
>    @Inject
>    private UserDAO userDAO;
>
> ...
>    Object onSuccess()
>    {
>        userDAO.add(user);
>
>        return UserAdmin.class;
>    }
> }
>
> public class UserDAO
> {
>    @Inject
>    private Session session;
>
>    void add(User user)
>    {
>        session.persist(user);
>    }
> }
>
>
> I tried to use @CommitAfter in my DAO but that didn't work. I essentially
> want to simulate:
>
>    void add(User user)
>    {
>        session.beginTransaction();
>        session.persist(user);
>        sessino.getTransaction().commit();
>    }
>
> but I don't want to explicitly code this in my DAO for fear that I may have
> some instances where the the transactional boundaries must be larger.
>
> Maybe this isn't a Tapestry specific thing - but I'm not sure how to inject
> the Hibernate Session into my DAO but have my Tapestry Controller define
> the
> transactional boundaries ... was hoping some Tapestry annotations might
> help
> me out.
>
> -Luther
>



-- 
James A. Hillyerd <ja...@hillyerd.com>