Wednesday, June 2, 2010

Hibernate property level lazy fetch

lazy properties could be values like name , that are typically mapped to a single column. This is rarely an important feature to enable (this, unlike other lazy fetching strategies in Hibernate 3, is disabled by default).
class employee{
.....
@Basic(fetch=FecthType.Lazy)
String name;
...
}
will not ensure that name is lazy loaded when we query for employee record.

By default, property level lazy loading is silently ignored by hibernate.
To enable property level lazy fetching, your classes have to be instrumented: bytecode is added to the original class to enable such feature.


Add these to the pom.xml

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.0.GA</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<tasks>
<taskdef name="instrument" classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
<classpath>
<path refid="maven.runtime.classpath" />
<path refid="maven.plugin.classpath" />
</classpath>
</taskdef>
<instrument verbose="false">
<fileset dir="${project.build.outputDirectory}">
<include name="**/domain/**/*.class" />
</fileset>
</instrument>
</tasks>
</configuration>
</plugin>

and if there are any ant task error while maven build regarding slf4j,

add this to the pom.xml file
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>

No comments:

Post a Comment