3.3.  DAO Support (experimental)

provides a very simple DAO support for web applications that extends the Spring Hibernate Dao support.

This DAO support consists of a simpler and more specific configuration language in the application context configuration file. Here is an example:

Example 3.2. Wedge Dao Configuration

<dao:configuration>

	<dao:sessionFactory name="sessionFactory"> 1
		<dao:dataSource name="dataSource"> 2
			<dao:jdbc user="${jdbc.username}" 3
					url="${jdbc.url}" 
					password="${jdbc.password}" 
					driver="${jdbc.driverClassName}"/>
		</dao:dataSource>
		<dao:hibernate-properties> 4
			<dao:sql 5
				dialect="org.hibernate.dialect.MySQLDialect" 6
				showSql="true" 
				hbm2ddlAuto="update" formatSql="false" />
		</dao:hibernate-properties>
	</dao:sessionFactory>
	
	<dao:services 7
				package="wedge.eg.testapp.dao"	8
				modelPackage="wedge.eg.testapp.model">
		<dao:service model="[model entity name]" /> 9
		...
	</dao:services>	
	
</dao:configuration>
1

Declaration of a session factory. The name attribute is optional as it defaults to 'sessionFactory'.

2

Declaration of data source to be used by the enclosing session factory. The name attribute is optional as it defaults to 'dataSource'

3

Declaration of a JDBC data source. For the time being this is the only data source type supported. JNDI coming soon!

4

(Work in progress) Very specific Hibernate framework configuration. Subelements coresponds to group of properties which are realted. For example, sql configuration, transaction, byte code enhancement, caching etc., all are grouped into specific elements. The actual properties are declared in the attributes of these elements. The name of the attributes are in general the camel case form of the coresponding Hibernate attributes.

5

One of the most used category of Hibernate properties.

6

The declaration of the database SQL dialect to use. Official supported dialects are declared as possible values of this attribute, thus in a XML editor you may get code completion for it!

7

Declaration of the DAO services.

8

The package where the services classes are declared. Full qualified names will be computed by using this as a base package.

9

Declaration of a DAO service. Note that you do not have do wire it with a session factory because will do that implicitly.

Also, provides a base DAO interface and a base DAO class that declare the basic CRUD operations that are common to all DAO services. This classes are declared by using the Java™ language generics and thus, may be made used in a very specific manner by declaring the target model class. Here is the declaration of the DAO generic interface provided by .

Example 3.3.  Generic DAO interface

package net.sf.wedge.dao;

public interface GenericDao<T, PK extends Serializable> {

	/**
	 * @param newPersistanceObject
	 * @return the primaryKey of the new persisted objected
	 */
	public PK create(T newPersistanceObject);	
	
	/**
	 * @param primaryKey
	 * @return the persistant instance of the object with primary key specified
	 */
	public T retrieve(PK primaryKey);
	
	/**
	 * @param persistentObject
	 * @return true if the object have been updated
	 */
	public boolean update(T persistentObject);
	
	/**
	 * @param persistentObject
	 * @return true if the object have been deleted
	 */
	public boolean delete(T persistentObject);
	
	/**
	 * @return a list with all items
	 */
	public List<T> findAll();
	
}

Your DAO service interfaces may extend this interface and have a more specific declaration based on a model class. Implementation of the services that extends net.sf.wedge.dao.GenericDao may extend (if you don't want to reimplement the interface yourself) the default implementation provided by which is net.sf.wedge.dao.WedgeGenericDao.

This generic implementation uses the name of model class that is associated with to perform the basics operation declared in the generic dao interface.

If you declare the DAO service in the dao configuration element then the framework will know for which model entity this service is declared and will set the modelClass property to be injected by the Spring IoC container. Also, the session factory that is needed by the DAO service implementation.