MainOverviewWikiIssuesForumBuildFisheye

Chapter 26. DAO Support

26.1. Dao and Template

Compass::Spring uses the CompassTemplate and CompassCallback classes provided by Compass::Core module as part of it's DAO (Data Access Object) support for Spring.

Compass::Spring provides a simple base class called CompassDaoSupport which can be initialized by Compass or CompassTemplate and provides access to CompassTemplate from it's subclasses.

The following code shows a simple Library Dao:

public class LibraryCompassDao extends CompassDaoSupport {
    public int getNumberOfHits(final String query) {
       Integer numberOfHits = (Integer)getCompassTemplate().execute(
           new CompassCallback() {
               public Object doInCompass(CompassSession session) {
                  CompassHits hits = session.find(query);
                  return new Integer(hits.getLength());
               }
           }
       );
    }
    return numberOfHits.intValue();
}

The following is an example of configuring the above Library DAO in the XML application context (assuming that we configured a LocalCompassBean named "compass" previously:

<beans>
   <bean id="libraryCompass" class="LibraryCompassDao">
      <property name="compass">
         <ref local="compass" />
      </property>
   </bean>
</beans>