# Integration with Spring ORM for Castor JDO

This guide will show you how to enable the use of JPA annotations with Castor JDO in the context of Spring, Spring ORM and the existing Spring ORM support for Castor JDO.

## A typical sample

Let's look at a typical Spring configuration file that shows how to use Castor JDO with Spring as a Spring ORM provider.

spring-config.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

        <!-- Enable transaction support using Annotations -->
        <tx:annotation-driven transaction-manager="transactionManager" />

        <bean id="transactionManager"
              class="org.castor.spring.orm.CastorTransactionManager">
              <property name="JDOManager" ref="jdoManager" />
        </bean>

        <bean id="jdoManager"
            class="org.castor.spring.orm.LocalCastorFactoryBean">
            <property name="databaseName" value="dbName" />
            <property name="configLocation" value="jdo-conf.xml" />
        </bean>

        <bean id="singleDao" class="SingleCastorDao">
            <property name="JDOManager">
                <ref bean="jdoManager"/>
            </property>
        </bean>
    </beans>

Above Spring application context configures the following Spring beans:

- A factory bean for JDOManager instantiation

- A Castor-specific transaction manager.

- The DAO implementation as shown above.

As shown above, the bean definition for the `JDOManager` factory bean points to a Castor JDO configuration file (`jdo-conf.xml`), whose content is shown below:

jdo-conf.xml

    <!DOCTYPE jdo-conf PUBLIC "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN""http://castor.org/jdo-conf.dtd">
    <jdo-conf>
      <database name="dbName" engine="mysql">
         <driver url="jdbc:mysql://localhost:3306/single"
                 class-name="com.mysql.jdbc.Driver">
            <param name="user" value="user" />
            <param name="password" value="password" />
         </driver>
         <mapping href="mapping-empty.xml" />
      </database>
      <transaction-demarcation mode="local" />
    </jdo-conf>

More on how to configure the Spring ORM provider for Castor JDO can be found at TBD.

## Adding a `JDOClassDescriptorResolver` configuration

In order to use JPA-annotated classes with the Spring ORM provider for Castor JDO, you will have to use and configure a `JDOClassDescriptorResolver` through an additional bean definition and link it to your `JDOManager` bean factory definition.

    <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:tx="http://www.springframework.org/schema/tx"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

        ...

        <bean id="classDescriptorResolver"
              class="org.castor.spring.orm.ClassDescriptorResolverFactoryBean">
            <property name="classes">
                <list>
                    <value>org.castor.jpa.test.Single</value>
                </list>
            </property>
        </bean>

        ...

        <bean id="jdoManager"
            class="org.castor.spring.orm.LocalCastorFactoryBean">
            <property name="databaseName" value="dbName" />
            <property name="configLocation" value="jdo-conf.xml" />
            <property name="classDescriptorResolver" ref="classDescriptorResolver" />
        </bean>

        ...
    </beans>

where ....

- Defines a `JDOClassDescriptorResolver` bean enlisting all the Java (domain) classes that carry JPA annotations.

- links the `JDOClassDescriptorResolver` bean to the `classDescriptorResolver` property of the `JDOManager` bean definition.

If your domain classes share a set of packages, it is also possible to enlist those packages with the `JDOClassDescriptorResolver` bean, replacing the bean definition shown above as follows:

        <bean id="classDescriptorResolver"
              class="org.castor.spring.orm.ClassDescriptorResolverFactoryBean">
            <property name="packages">
                <list>
                    <value>org.castor.jpa.test</value>
                </list>
            </property>
        </bean>