Results 1 to 4 of 4

Thread: Alternative to open session in view pattern

  1. #1
    Join Date
    Oct 2004
    Location
    Antwerp, Belgium
    Posts
    96

    Default Alternative to open session in view pattern

    TouchingAfterReturningAdvice offers an alternative to the open session in view pattern in that it touches configurable properties on the return value of method invocations. This advice is available in the Spring Modules sandbox.

    If the return value is an instance of java.util.Collection or Object[] all its elements will be touched with the configured properties. If no properties have been specified collections will still be iterated over.

    An example:

    The service:

    Code:
    public interface CustomerService {
       public Collection findAllCustomers();
    }
    The domain class:

    Code:
    public class Customer {
       private Collection pendingOrders = null;
       // this property is configured for lazy loading.
       public Collection getPendingOrders() {
          return this.pendingOrders;
       }
       private Collection completedOrders = null;
       // this property is configured for lazy loading.
       public Collection getCompletedOrders() {
          return this.completedOrders;
       }
    }
    The Spring config:

    Code:
    <bean id="customerService" parent="transactionedBean">
       <property name="target">
          <bean class="....DefaultCustomerService" autowire="byType"/>
       </property>
       <property name="postInterceptors">
          <list>
             <bean class="....NameMatchMethodPointcutAdvisor">
                <property name="mappedNames">
                   <value>findAllCustomers</value>
                </property>
                <property name="advice">
                   <bean class="org.springmodules.aop.framework.TouchingAfterReturningAdvice">
                      <property name="properties">
                         <list>
                            <value>pendingOrders</value>
                            <map>
                               <entry key="completedOrders">
                                  <list>
                                     <value>products</value>
                                  </list>
                               </entry>
                            </map>
                         </list>
                      </property>
                   </bean>
                </property>
             </bean>
          </list>
       </property>
    </bean>

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    This makes sense. In fact we were discussing the possibility of this in last week's Core Spring training in London :-)

    It might be an idea to try to simplify the configuration through a convenient generic Advisor that, for example, took a set of OGNL expressions.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    One thing to note however: fetch joins may be more efficient if you want to ensure the most efficient database querying. More invasive, however: the declarative touching approach is definitely simpler. It would also work for any ORM tool, so it's not tied to Hibernate, which is a plus.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  4. #4
    Join Date
    Oct 2004
    Location
    Antwerp, Belgium
    Posts
    96

    Default

    I've added support for OGNL expressions and created a convenience class TouchingNameMethodMatchAdvisor. If collections are returned you can use the #returned variable in OGNL expressions:

    Code:
    <bean id="customerService" parent="transactionedBean">
       <property name="target">
          <bean class="....DefaultCustomerService" autowire="byType"/>
       </property>
       <property name="postInterceptors">
          <list>
             <bean class="org.springmodules.aop.framework.TouchingNameMatchMethodAdvisor">
                <property name="mappedNames">
                   <value>findAllCustomers</value>
                </property>
                <property name="advice.ognl">
                   <list>
                      <value>#returned.&#123;pendingOrders.size&#125;</value>
                      <value>#returned.&#123;completedOrders.&#123;products.size&#125;&#125;</value>
                   </list>
                </property>
             </bean>
          </list>
       </property>
    </bean>

Similar Threads

  1. Replies: 11
    Last Post: Jun 1st, 2006, 04:30 PM
  2. Hibernate Long Session Per Flow?
    By akw in forum Web Flow
    Replies: 21
    Last Post: Dec 12th, 2005, 08:06 PM
  3. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  4. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •