Data Access Objects
The interface info.joseluismartin.dao.Dao extends PageableDataSource adding the classic CRUD operations (create, retrieve, update, delete) upon entities.
Whilst some developers rather use generic DAOs, others prefer just generalize retrieve methods. Both styles are available in the Dao interface which provides the following entity retrieving generic methods:
- <T> T get(PK id, Class<T> clazz)
- <T> getAll(PK id, Class<T> clazz)
As well as the default entity retrieving methods.
The three Dao interface implementations included in JDAL are thread safe, so they are usually deployed into the application context as singletons.
Although JDAL may be used without the Spring Framework IoC container, the library has been designed folllowing the Dependency Injection principles, being consequently easier using it with the IoC container.
JDAL Core defines the namespace http://www.jdal.org/schema/jdal which simplifies the definition of persistence services in the container beans definition file of Spring Framework.
For example, In order to provide a DAO for the entity org.example.Entity, you just need to declare the service in the application context file:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdal="http://www.jdal.org/schema/jdal" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.jdal.org/schema/jdal http://www.jdal.org/schema/jdal/jdal-core.xsd" default-init-method="init"> <!-- Daos --> <jdal:service entity="org.example.Entity" /> </beans>
Often, filtering methods should be provided when requesting data from a repository. JDAL also offers two user objects for getting paged results from persistence services: Filter y CriteriaBuilder.
Most of the times, these are just the two classes that the user needs to code in the application data access layer. I've put special interest on simplifying this application specific task as much as possible.
DAO interface implementations contain a map associating CriteriaBuilders with a name (String). CriteriaBuilders create Criteria instances, if using Hibernate or CriteriaQueries given the case we are using JPA.
The Filter interface provides the filtering parameters for building each Criteria type. The selection of the CriteriaBuilder associated which each filter type is done by the filter name (ie, filter.getFilterName()).
BeanFilter class is a base class for filters created as simple JavaBeans.
JpaCriteriaBuilderSupport and AbstractCriteriaBuilder classes provide templates intended to simplify CriteriaBuilder creation for JPA and Hibernate.
CriteriaBuilders can be declared into application context using the <jdal:criteria> element. This element specifies the name of the CriteriaBuilder into the name attribute and a reference to a context defined bean into the builder attribute.
CriteriaBuilders are not the only way to get filtered results. On behalf of DAO type other implementation dependent procedures are supported.
As an example follows HibernateDao algorithm when a filtered page request is received.
- The filter is a persistent object: HibernateDao will execute a query as Session.findByExample() method.
- The filter is an instance of Filter: In this case the next actions are tested sequentially:
- filter.getFilterName() matches with the name of an hibernate declared query: The query is executed aplying the parameters from filter.getParameterMap()
- filter.getFilterName() matches with the name of an hibernate declared filter: The filter is enabled using the parameters from filter.getParameterMap()
- filter.getFiterName()matches with the key of a CriteriaBuilder implementation in CriteriaBuilderMap: The query from the Criteria object returned by CriteriaFilterBuilder.build(criteria, filter) is executed.
- Finally, the DAO will try to invoke the method "createCriteria" + filter.getFilterName()' in order to build a Criteria object which will provide the query.
When using JpaDao, if the filter name matches with a named query, the query will be executed using the parameters from filter.getParamenterMap()