This package is designed to make it easier and faster for developers to write their DAOs without having to rewrite the same old boring save/delete/etc functions over and over for each persistent type but also not having to have implementation dependencies in their DAO interfaces. It also allows for good control over which persistent objects are usable within the DAO and is easy to extend so you can add your own DAO methods.

Usage may seem complex at first but it is actually not too bad and is highly configurable.

There are 3 ways to use this package depending on your needs.
A) {@link org.sakaiproject.genericdao.api.BasicGenericDao} use (minimal methods)
B) {@link org.sakaiproject.genericdao.api.GeneralGenericDao} use (more advanced methods)
C) Extended Dao use (advanced methods plus your own)
Specific steps for each method are outlined below.

Using the jar package in your web application:
This is fairly easy and is just like using any other jar with one BIG excpetion which I mention below.

The jar is located in the following maven 2 repository for now:
https://source.sakaiproject.org/maven2/

Here is the direct link to the location of the package:
https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/

You just need to add the following dependency to the your maven pom.xml file where the package is accessed (probably your service or dao): (replace the version number with the correct current one for the package)

<!-- generic DAO --> <dependency> <groupId>org.sakaiproject</groupId> <artifactId>generic-dao</artifactId> <version>1.0</version> <url>https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/</url> </dependency>

NOTE: Here is the big exception:
If you are using this in Sakai and using Hibernate, you will need to force the jar to be placed into shared. This is because the DAO interface must be in shared and hibernate is in shared. Just use the following in your logic/dao pom instead of the general use above. (if you are using it with JDBC or not using the shared hibernate session factory then just use the method above)

<!-- generic DAO --> <dependency> <groupId>org.sakaiproject</groupId> <artifactId>generic-dao</artifactId> <version>${sakai.generic-dao.version}</version> <url>https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/</url> <scope>provided</scope> </dependency>

NOTE: We recommend you look at the auotmated tests in the source as well (/src/test), they contain sample spring config files and sample persistent objects and configurations and are a good place to start if you want sample code, this is especially important if you are using JDBC since the examples below are of use in hibernate

A) {@link org.sakaiproject.genericdao.api.BasicGenericDao} use
If you want to use the {@link org.sakaiproject.genericdao.api.BasicGenericDao} which has fairly simple but most likely high performance functionality (save/delete/find/etc) then you should do the following in your spring config file (components.xml in Sakai):

  • NOTE: Replace "*yourappname*" with your application name
  • NOTE: Replace "*YourObject*" with your persistent object(s)
  • 1) Update your spring config file for hibernate usage (for these examples we will assume you are using the Hibernate implementations):
    spring config xml example:

    <!-- Add your HBM files to the Sakai global session factory --> <bean id="org.sakaiproject.*yourappname*.AdditionalHibernateMappingsImpl" class="org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl"> <property name="mappingResources"> <list> <value>org/sakaiproject/*yourappname*/hbm/*YourObject*.hbm.xml</value> </list> </property> </bean>
    OR if you are not using Sakai:
    Note that you will need to use the SessionFactory and TransactionManager that you created here instead of the Sakai ones used in the examples below.
    <!-- create a data source --> <bean id="*yourappname*DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.hsqldb.jdbcDriver</value> </property> <property name="url"> <value>jdbc:hsqldb:.</value> </property> <property name="username"> <value>sa</value> </property> <property name="password"> <value></value> </property> </bean> <!-- create a SessionFactory bean from the data source and point it at our HBMs --> <bean id="*yourappname*SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="testDataSource" /> <property name="mappingResources"> <list> <value>org.sakaiproject.*yourappname*.model.*YourObject*</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.query.substitutions">true 1, false 0</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <!-- Create a transaction manager from the SessionFactory --> <bean id="*yourappname*TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="*yourappname*SessionFactory" /> </bean>

    2) Update the remainder of the spring config file to include the necessary beans for the DAO

    <!-- create the DAO itself but as a target --> <bean id="org.sakaiproject.*yourappname*.dao.GenericDaoTarget" class="org.sakaiproject.genericdao.hibernate.HibernateBasicGenericDao"> <property name="sessionFactory" ref="org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory" /> <property name="persistentClasses"> <list> <value>org.sakaiproject.*yourappname*.model.*YourObject*</value> </list> </property> </bean> <!-- put the DAO in a transaction proxy --> <bean id="org.sakaiproject.*yourappname*.dao.GenericDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager" /> <property name="target" ref="org.sakaiproject.*yourappname*.dao.GenericDaoTarget" /> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>

    3) Inject the GenericDao into your application logic bean. To do this you will need to add the following bean to your spring config file:

    <bean id="org.sakaiproject.*yourappname*.logic.YourAppLogic" class="org.sakaiproject.*yourappname*.logic.impl.YourAppLogicImpl"> <property name="genericDao" ref="org.sakaiproject.*yourappname*.dao.GenericDao" /> </bean>

    4) Modify your logic classes to accept the injected dao and then you can access the simple DAO methods. Here is an example of how to do that:

    If this is one of your application logic classes:

    	public class YourAppLogicImpl {
    	
    		private BasicGenericDao genericDao;
    		public void setGenericDao(BasicGenericDao genericDao) {
    			this.genericDao = genericDao;
    		}
    	
    		public void someMethod() {
    			YourObject yo = new YourObject("some text");
    			genericDao.save(yo);
    		}
    	
    	}
    

    B) {@link org.sakaiproject.genericdao.api.GeneralGenericDao} use
    If you decide you need more functionality that the {@link org.sakaiproject.genericdao.api.BasicGenericDao} provides, then it is easy to make the {@link org.sakaiproject.genericdao.api.GeneralGenericDao} available to your application logic.

    1) Define your HBMs and the GeneralGenericDao bean in your spring config file.
    Here is an example of the spring config file:

    <!-- Add your HBM files to the Sakai global session factory --> <bean id="org.sakaiproject.*yourappname*.AdditionalHibernateMappingsImpl" class="org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl"> <property name="mappingResources"> <list> <value>org/sakaiproject/*yourappname*/hbm/*YourObject*.hbm.xml</value> </list> </property> </bean> <!-- Now create the bean for the complex DAO (using the all-in-one anonymous inner bean syntax) --> <bean id="org.sakaiproject.*yourappname*.dao.GeneralGenericDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager" /> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> <property name="target"> <bean class="org.sakaiproject.genericdao.hibernate.HibernateGeneralGenericDao" init-method="init"> <property name="sessionFactory" ref="org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory" /> <property name="persistentClasses"> <list> <value>org.sakaiproject.*yourappname*.model.*YourObject*</value> </list> </property> </bean> </property> </bean>

    2) Inject the {@link org.sakaiproject.genericdao.api.GeneralGenericDao} into your application logic bean. To do this you will need to add the following bean to your spring config file:

    <bean id="org.sakaiproject.*yourappname*.logic.YourAppLogic" class="org.sakaiproject.*yourappname*.logic.impl.YourAppLogicImpl"> <property name="genericDao" ref="org.sakaiproject.*yourappname*.dao.GeneralGenericDao" /> </bean>

    3) Modify your application logic bean to work with the injection. Note that {@link org.sakaiproject.genericdao.api.GeneralGenericDao} provides access to the same functions as {@link org.sakaiproject.genericdao.api.BasicGenericDao} along with a large number of additional classes.

    For example, if this is one of your application logic classes:

    	public class YourAppLogicImpl {
    	
    		private GeneralGenericDao genericDao;
    		public void setGenericDao(GeneralGenericDao genericDao) {
    			this.genericDao = genericDao;
    		}
    	
    		public void someMethod() {
    			YourObject yo = new YourObject("some text");
    			genericDao.save(yo);
    		}
    	
    	}
    

    C) Extended Dao use
    If you decide you want to extend the {@link org.sakaiproject.genericdao.api.GeneralGenericDao} then it is not to difficult. Just create a new interface that extends {@link org.sakaiproject.genericdao.api.GeneralGenericDao} and a class that extends {@link org.sakaiproject.genericdao.hibernate.HibernateGeneralGenericDao} or whichever implementation of {@link org.sakaiproject.genericdao.api.GeneralGenericDao} that you want.

    1) Create the new interface for you application Dao:

    	import org.sakaiproject.genericdao.api.GeneralGenericDao;
    	public interface YourAppDao extends GeneralGenericDao {
    	}
    

    2) Create the implementation of your new interface:

    	import org.sakaiproject.*yourappname*.dao.YourAppDao;
    	import org.sakaiproject.genericdao.hibernate.HibernateGeneralGenericDao;
    	public class YourAppDaoImpl
    		extends HibernateGeneralGenericDao
    			implements YourAppDao {
    	}
    

    3) Update the spring config file so that you replace the {@link org.sakaiproject.genericdao.api.GeneralGenericDao} bean (or add this if you do not have one) with something like this:

    <bean id="org.sakaiproject.*yourappname*.dao.YourAppDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager" /> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> <property name="target"> <bean class="org.sakaiproject.*yourappname*.dao.YourAppDaoImpl" init-method="init"> <property name="sessionFactory" ref="org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory" /> <property name="persistentClasses"> <list> <value>org.sakaiproject.*yourappname*.model.*YourObject*</value> </list> </property> </bean> </property> </bean>

    4) To inject your new DAO into the logic bean, you will need to add (or update) the logic bean in your spring config file:

    <bean id="org.sakaiproject.*yourappname*.logic.YourAppLogic" class="org.sakaiproject.*yourappname*.logic.impl.YourAppLogicImpl"> <property name="genericDao" ref="org.sakaiproject.*yourappname*.dao.YourAppDao" /> </bean>

    5) Update your logic class to use your DAO class type:

    	public class YourAppLogicImpl {
    	
    		private YourAppDao genericDao;
    		public void setGenericDao(YourAppDao genericDao) {
    			this.genericDao = genericDao;
    		}
    	
    		public void someMethod() {
    			YourObject yo = new YourObject("some text");
    			genericDao.save(yo);
    		}
    	
    	}
    

    Note: If you decide that you want to hibernate lazy init you will need to create and use a wrapper class and a set of beans as shown below. The method shown works in RSF. If you are using some other presentation layer you will have to adapt it.
    Sample Wrapper Class:

    package org.sakaiproject.genericdao.wrapper;
    
    import uk.org.ponder.util.RunnableInvoker;
    import org.sakaiproject.*yourappname*.dao.YourAppDao;
    
    /**
     * This wraps our dao so that it can be accessed lazily
     * @author Aaron Zeckoski (aaronz@vt.edu)
     */
    public class ModelAccessWrapperInvoker implements RunnableInvoker {
    
    	private YourAppDao genericDao;
    	public void setGenericDao(YourAppDao genericDao) {
    		this.genericDao = genericDao;
    	}
    
    	/* (non-Javadoc)
    	 * @see uk.org.ponder.util.RunnableInvoker#invokeRunnable(java.lang.Runnable)
    	 */
    	public void invokeRunnable(Runnable toinvoke) {
    		genericDao.invokeTransactionalAccess(toinvoke);
    	}
    
    }
    

    Then you will need to add some wrapper classes to the RSF applicationContext.xml like so: