Property Lazy Fetching

Property lazy fetching can be really useful especially when you have a field which is binary data and you don’t want to fetch this property every time the object is loaded. Lets say you have an entity class Foo as below.

package entities;
public class Foo {
	private String id;
	private String filename;
	private byte[] filedata;
}

and the mapping file is as follows

<hibernate-mapping package="entities">
        <class name="Foo">
		<id name="id" column="id" length="36">
			<generator class="uuid" />
		</id>
		<property name="filename" />
		<property name="filedata" lazy="true"/>
	</class>
</hibernate-mapping>

Here as you may have noticed filedata property is marked as lazy=”true”. However this attribute is ignored by Hibernate unless the class is bytecode instrumented. Therefore to enable lazy fetching for properties the classes must be instrumented. Luckily there is an ant task for this and if you use maven for the build process you can call it simply by calling maven-ant-plugin.

   <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <executions>
           <execution>
               <id>instrument</id>
                   <phase>compile</phase>
                       <configuration>
                        <tasks>
                           <echo>Instrumenting classes</echo>
                           <taskdef name="instrument" classname="org.hibernate.tool.instrument.cglib.InstrumentTask">
                               <classpath>
                                   <path refid="maven.dependency.classpath" />
                                   <path  refid="maven.plugin.classpath" />
                               </classpath>
                           </taskdef>
                          <instrument verbose="true">
                             <fileset dir="${project.build.outputDirectory}">
                                <include name="**/entities/Foo.class" />
                             </fileset>
                          </instrument>
                    </tasks>
                    </configuration>
                    <goals>
                            <goal>run</goal>
                    </goals>
               </execution>
           </executions>
           <dependencies>
               <dependency>
                       <groupId>org.hibernate</groupId>
                       <artifactId>hibernate</artifactId>
                       <version>3.2.1.ga</version>
               </dependency>
          </dependencies>
          </plugin>

Comments (1)

Spring MVC and Restlet Integration

The first thing needs to be done is adding RestletFrameworkServlet to web.xml as follows :

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext-business.xml
        </param-value>
    </context-param>

    <!-- Loads the root application context of this web app at startup -->
    <listener>
       <listener-class>
              org.springframework.web.context.ContextLoaderListener
       </listener-class>
   </listener>


    <!-- Spring Dispatcher Servlet which handles http requests
          and invokes the appropriate controller -->
   <servlet>
        <servlet-name>springrest</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springrest</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>


    <!-- Restlet adapter -->
    <servlet>
        <servlet-name>rservlet</servlet-name>
        <servlet-class>
             com.noelios.restlet.ext.spring.RestletFrameworkServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Catch all request URIs starting with  prefix /rest-->
    <servlet-mapping>
        <servlet-name>rservlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

RestletFrameworkServlet extends from FrameworkServlet which is basically created to provide integration with Spring application context. Therefore for the requests ending with .html suffix are handled by Dispatcher Servlet and the requests starting with the prefix /rest are handled by RestletFrameworkServlet. Service definitions for the business logic are kept in applicationContext.business.xml file which is loaded at the startup of the application and all the bean definitions in this file are accessible from both Spring controllers and Restlet resources.

The next should be done is creating the rservlet-servlet.xml file which will contain restlets and resources in it.This file is parsed by RestletFrameworkServlet.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="root" class="org.restlet.ext.spring.SpringRouter">
        <property name="attachments">
            <map>
                <entry key="/users/{user}">
                    <bean class="org.restlet.ext.spring.SpringFinder">
                        <lookup-method name="createResource" bean="userResource" />
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="userResource" class="net.springrest.user.UserResource" scope="prototype" >
	<!-- userService is a service which is used by both your web controller and rest resource
                and it is defined in applicationContext-business.xml file -->
    	<property name="userService" ref="userService" />
    </bean>

</beans>

Comments (1)

« Newer Posts · Older Posts »