Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question consists of 2 parts:</p> <p><strong>How to use hibernate inside tapestry managed services?</strong></p> <p>You can inject hibernate session in your service using <code>@Inject</code> annotation:</p> <pre><code>@Inject private Session session; </code></pre> <p>or as a constructor parameter:</p> <pre><code>public MyService buildMyService(Session session) { return new MyAwsomeService(session); } </code></pre> <p>Then you can use it inside service. This session is managed by tapestry as a perthread service. It will be opened on first usage and be closed after end of request.</p> <p><strong>How to use hiberanate transations inside tapestry managed services?</strong></p> <p>You can do this manually using injected <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/hibernate/HibernateSessionManager.html" rel="nofollow">HibernateSessionManager</a>:</p> <pre><code>@Inject private HibernateSessionManager manager; public void doSomeWork(Entity entity) { try { session.delete(entity); manager.commit(); } catch (Exception e) { manager.abort(); } } </code></pre> <p>Or you can annotate all transactional methods with <code>@CommitAfter</code> and <a href="http://tapestry.apache.org/service-advisors.html" rel="nofollow">advise services</a> using <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/hibernate/HibernateTransactionAdvisor.html" rel="nofollow">HibernateTransactionAdvisor</a>:</p> <pre><code>public interface MyService { @CommitAfter void doSomeWork(Entity entity); } public class MyTapestryModule { @Match("*Service") public void adviseTransactions(HibernateTransactionAdvisor advisor, MethodAdviceReceiver receiver) { advisor.addTransactionCommitAdvice(receiver); } } </code></pre> <p>Or you can <a href="http://tapestry.apache.org/tapestry-ioc-decorators.html" rel="nofollow">decorate sevices</a> using <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/hibernate/HibernateTransactionDecorator.html" rel="nofollow">HibernateTransactionDecorator</a>:</p> <pre><code>public class MyTapestryModule { @Match("*Service") public &lt;T&gt; T decorateTransactions(Class&lt;T&gt; serviceInterface, T delegate, String serviceId, HibernateTransactionDecorator decorator) { return decorator.build(serviceInterface, delegate, serviceId); } } </code></pre> <p>How it works. It will start new transaction before method invocation and try to commit after. If any error occurs during method invocation this transaction will be rolled back. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload