src.main.java.overview.html Maven / Gradle / Ivy
Show all versions of generic-dao Show documentation
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.
The package includes functionality for all the basic ORM CRUD type methods along with
search methods and batch methods. The JDBC part of the package includes support for caching
all the DAO methods (which could also be used with the hibernate part but hibernate has its own
caching so you should probably use that). It also includes interceptor points for before and
after all read and write methods. For simpler use cases, you can write your POJOs, make
them persistent, create your DDL and not have to write a single line of DAO code.
The package is built on and depends on the spring framework.
The main website is currently here: Generic DAO Package
Usage may seem complex at first but it is actually fairly simple 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 a few exceptions for Sakai developers which I mention below).
Either drop the jar into your war file or use maven 2 to include it (as shown below).
The jar package includes all source code which you are free to modify if you desire.
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,
check the sakaiproject maven 2 repo for the latest tag)
org.sakaiproject
generic-dao
1.0
https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/
NOTE: For users of previous versions who are working in Sakai:
It is no longer required to put generic-dao in the tomcat shared lib area and in fact you should not do that anymore.
The correct way to handle this in Sakai is demonstrated in the
blogwow app.
Here are the conversion steps:
- Move your DAO api from your shared API into your IMPL (component implementation)
- In your component bean for your DAO transaction handling, change
org.springframework.transaction.interceptor.TransactionProxyFactoryBean
to org.sakaiproject.genericdao.springutil.CurrentClassLoaderTxProxyFactoryBean
You will still need to put your persistent objects and HBMs in shared or in the same classloader
as the hibernate SessionFactory.
org.sakaiproject
generic-dao
${sakai.generic-dao.version}
https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/
provided
NOTE: We recommend you look at the automated 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)
Spring JDBC Usage:
Create your DAO bean and provide it with a datasource and the configuration settings. In this example,
autoDDL indicates the the DDL statements should be run, if this is false they will not be executed.
databaseType indicates the type of database that you are using, this should be one of the type keys
(e.g. HSQLDB, MYSQL, ORACLE, etc.)
The key thing is to define your persistent object by using data mappers. One mapper for each persistent object.
You can define a mapper programmatically by implementing the DataMapper interface or just use the SimpleDataMapper
and put your entire configuration in spring as shown below.
NOTE: Sakai:
If you are using Sakai then you can use the built in beans from the EntityBroker project to easily
get the auto.ddl and database types from the Sakai SQLService and ServerConfigurationService.
...
...
Here is another example of generic-dao using JDBC but this time using custom written DataMapper classes.
This time the dao bean is wrapped in a transaction proxy which uses a transaction manager bean.
The dao bean also includes optional interceptor and caching beans.
PROPAGATION_REQUIRED
NOTE: If you are using Sakai then you must org.sakaiproject.genericdao.springutil.CurrentClassLoaderTxProxyFactoryBean
from the generic-dao utils instead of org.springframework.transaction.interceptor.TransactionProxyFactoryBean.
Hiberate Usage:
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:
org/sakaiproject/*yourappname*/hbm/*YourObject*.hbm.xml
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.
org.hsqldb.jdbcDriver
jdbc:hsqldb:.
sa
org.sakaiproject.*yourappname*.model.*YourObject*
org.hibernate.dialect.HSQLDialect
false
org.hibernate.cache.EhCacheProvider
true 1, false 0
create
2) Update the remainder of the spring config file to include the necessary
beans for the DAO
org.sakaiproject.*yourappname*.model.*YourObject*
PROPAGATION_REQUIRED
NOTE: If you are not using Sakai then you can either use the CurrentClassLoaderTxProxyFactoryBean or
the traditional org.springframework.transaction.interceptor.TransactionProxyFactoryBean here,
either one is fine. If you are using Sakai then you MUST use the CurrentClassLoaderTxProxyFactoryBean as shown
because of the multi classloader environment of Sakai
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:
org/sakaiproject/*yourappname*/hbm/*YourObject*.hbm.xml
PROPAGATION_REQUIRED
org.sakaiproject.*yourappname*.model.*YourObject*
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:
PROPAGATION_REQUIRED
org.sakaiproject.*yourappname*.model.*YourObject*
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 use hibernate lazy loading 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. I do not
recommend you depend on lazy loading in your usage though.
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 ([email protected])
*/
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: