MainOverviewWikiIssuesForumBuildFisheye

Chapter 28. Jdbc Gps Device Support

28.1. Introduction

This section provides no additional implementation, only samples of using Jdbc Gps Device within Spring IOC container.

The database structure is the same one as the one on the Jdbc Gps Device section, and is show here as well:

CREATE TABLE parent (
    id INTEGER NOT NULL IDENTITY PRIMARY KEY,
    first_name VARCHAR(30),
    last_name VARCHAR(30),
    version BIGINT NOT NULL
);
CREATE TABLE child (
    id INTEGER NOT NULL IDENTITY PRIMARY KEY,
    parent_id INTEGER NOT NULL,
    first_name VARCHAR(30),
    last_name VARCHAR(30),
    version BIGINT NOT NULL
);
alter table child add constraint
    fk_child_parent foreign key (parent_id) references parent(id);

28.2. ResultSet Mapping

A configuration sample of a the ResultSet mapping given at the Jdbc Gps Device section is shown here in a Spring configuration file (taken from the unit tests):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="rsMapping" class="org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping">
      <property name="alias"><value>result-set</value></property>
      <property name="indexUnMappedColumns"><value>true</value></property>
      <property name="selectQuery"><value>
        select  p.id as parent_id,
                        p.first_name as parent_first_name,
                        p.last_name as parent_last_name,
                        p.version as parent_version,
                COALESCE(c.id, 0) as child_id,
                c.first_name as child_first_name,
                c.last_name child_last_name,
                COALESCE(c.version, 0) as child_version
        from
                parent p left join child c on p.id = c.parent_id
      </value></property>
      <property name="versionQuery"><value>
        select  p.id as parent_id,
                        COALESCE(c.id, 0) as child_id,
                        p.version as parent_version,
                        COALESCE(c.version, 0) as child_version
        from
                parent p left join child c on p.id = c.parent_id
      </value></property>
      <property name="idMappings">
        <list>
          <bean class="org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping">
            <property name="columnName"><value>parent_id</value></property>
            <property name="propertyName"><value>parent_id</value></property>
            <property name="columnNameForVersion"><value>p.id</value></property>
          </bean>
          <bean class="org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping">
            <property name="columnName"><value>child_id</value></property>
            <property name="propertyName"><value>child_id</value></property>
            <property name="columnNameForVersion"><value>COALESCE(c.id, 0)</value></property>
          </bean>
        </list>
      </property>
      <property name="dataMappings">
        <list>
          <bean class="org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping">
            <property name="columnName"><value>parent_first_name</value></property>
            <property name="propertyName"><value>parent_first_name</value></property>
          </bean>
          <bean class="org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping">
            <property name="columnName"><value>child_first_name</value></property>
            <property name="propertyName"><value>child_first_name</value></property>
            <property name="propertyStoreString"><value>compress</value></property>
          </bean>
        </list>
      </property>
      <property name="versionMappings">
        <list>
          <bean class="org.compass.gps.device.jdbc.mapping.VersionColumnMapping">
            <property name="columnName"><value>parent_version</value></property>
          </bean>
          <bean class="org.compass.gps.device.jdbc.mapping.VersionColumnMapping">
            <property name="columnName"><value>child_version</value></property>
          </bean>
        </list>
      </property>
    </bean>

    <!-- Compass-->
    <bean id="compass" class="org.compass.spring.LocalCompassBean">
        <property name="mappingResolvers">
            <list>
                <bean class="org.compass.gps.device.jdbc.ResultSetResourceMappingResolver">
                  <property name="mapping"><ref local="rsMapping" /></property>
                  <property name="dataSource"><ref bean="dataSource" /></property>
                </bean>
            </list>
        </property>
        <property name="compassSettings">
            <props>
                <prop key="compass.engine.connection">target/testindex</prop>
                <!-- This is the default transaction handling (just explicitly setting it) -->
                <prop key="compass.transaction.factory">
                    org.compass.core.transaction.LocalTransactionFactory
                </prop>
            </props>
        </property>
    </bean>

     <bean id="jdbcGpsDevice" class="org.compass.gps.device.jdbc.ResultSetJdbcGpsDevice">
       <property name="name"><value>jdbcDevice</value></property>
       <property name="dataSource"><ref bean="dataSource" /></property>
       <property name="mirrorDataChanges"><value>true</value></property>
       <property name="mappings">
        <list>
          <ref local="rsMapping" />
        </list>
       </property>
     </bean>

     <bean id="gps" class="org.compass.gps.impl.SingleCompassGps"
                init-method="start" destroy-method="stop">
       <property name="compass"><ref bean="compass" /></property>
       <property name="gpsDevices">
         <list>
           <ref local="jdbcGpsDevice" />
         </list>
       </property>
       <property name="deleteIndexBeforeIndex"><value>true</value></property>
     </bean>

</beans>

28.3. Table Mapping

A configuration sample of a the Table mapping given at the Jdbc Gps Device section is shown here in a Spring configuration file (taken from the unit tests):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="parentMapping" class="org.compass.gps.device.jdbc.mapping.TableToResourceMapping">
      <property name="alias"><value>parent</value></property>
      <property name="tableName"><value>PARENT</value></property>
      <property name="indexUnMappedColumns"><value>true</value></property>
      <property name="versionMappings">
        <list>
          <bean class="org.compass.gps.device.jdbc.mapping.VersionColumnMapping">
            <property name="columnName"><value>version</value></property>
          </bean>
        </list>
      </property>
    </bean>

    <bean id="childMapping" class="org.compass.gps.device.jdbc.mapping.TableToResourceMapping">
      <property name="alias"><value>child</value></property>
      <property name="tableName"><value>CHILD</value></property>
      <property name="indexUnMappedColumns"><value>true</value></property>
      <property name="versionMappings">
        <list>
          <bean class="org.compass.gps.device.jdbc.mapping.VersionColumnMapping">
            <property name="columnName"><value>version</value></property>
          </bean>
        </list>
      </property>
    </bean>

    <!-- Compass-->
    <bean id="compass" class="org.compass.spring.LocalCompassBean">
        <property name="mappingResolvers">
            <list>
                <bean class="org.compass.gps.device.jdbc.ResultSetResourceMappingResolver">
                  <property name="mapping"><ref local="parentMapping" /></property>
                  <property name="dataSource"><ref bean="dataSource" /></property>
                </bean>
                <bean class="org.compass.gps.device.jdbc.ResultSetResourceMappingResolver">
                  <property name="mapping"><ref local="childMapping" /></property>
                  <property name="dataSource"><ref bean="dataSource" /></property>
                </bean>
            </list>
        </property>
        <property name="compassSettings">
            <props>
                <prop key="compass.engine.connection">target/testindex</prop>
                <!-- This is the default transaction handling (just explicitly setting it) -->
                <prop key="compass.transaction.factory">
                    org.compass.core.transaction.LocalTransactionFactory
                </prop>
            </props>
        </property>
    </bean>

     <bean id="jdbcGpsDevice" class="org.compass.gps.device.jdbc.ResultSetJdbcGpsDevice">
       <property name="name"><value>jdbcDevice</value></property>
       <property name="dataSource"><ref bean="dataSource" /></property>
       <property name="mirrorDataChanges"><value>true</value></property>
       <property name="mappings">
        <list>
          <ref local="parentMapping" />
          <ref local="childMapping" />
        </list>
       </property>
       <property name="snapshotPersister">
        <bean class="org.compass.gps.device.jdbc.snapshot.FSJdbcSnapshotPersister">
          <property name="path"><value>target/testindex/snapshot</value></property>
        </bean>
       </property>
     </bean>

     <bean id="gps" class="org.compass.gps.impl.SingleCompassGps"
                init-method="start" destroy-method="stop">
       <property name="compass"><ref bean="compass" /></property>
       <property name="gpsDevices">
         <list>
           <ref local="jdbcGpsDevice" />
         </list>
       </property>
       <property name="deleteIndexBeforeIndex"><value>true</value></property>
     </bean>

</beans>