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:
2) Update the remainder of the spring config file to include the necessary beans for the DAO
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:
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:
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:
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:
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:
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);
}
}