一 Spring DAO philosophy
1 Understanding Spring's DataAccesssException
Spring's DAO frameworks donot throw teechnology-specific exceptions such as SQLException
or HibernateeExcepiton.Instead ,all exceptions thrown are subclasses of DataAccessException
2 You are not forced to handle DataAccessExceptions
DataAccessException is a RuntimeException,so it si an unchecked exception.Since these are quite often unrecoverable,you are not forced to handle these exception.
Instead ,you can catch the exception if recovery is possible.since DataAccessException is not only a RuntimeException,but it subclasses Spring's NestedRuntimeException. This menas that the root Exception is alwarys via NestedRuntimeException's getCause() method.
3 Work with DataSources
a getting a Datasource from JNDI
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
b Creating a Datasource connection pool
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
c Using a DataSource while testing
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
4 Consistent DAO support
Spring template class handle the invariant part of data access-controling the trancsaction
manage resource,handling exception .Implementation of callback interface define what is specific to your application--creating statement,binding parameter and marshalling result set.
Spring separates the fixed an vaiant parts of data access process into tow distince classes:
template and callbacks.Template manage the fixed parts of the process while callback are where you fill in the implement details;
one the top of template-callback desing ,spring framework provide a support class which your own data access subclass it. And the support class already have a property for holding a template.
二 Integerating Hibernate with Spring
1 Managing Hibernate resources
you will keep a single instance of SessionFactory throughtout your application
<bean id="sessionFactory"class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
you also want to manager how hibernate is configured
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.
dialect.MySQLDialect</prop>
</props>
</property>
…
</bean>
and the last thing is whick map files is read
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>Student.hbm.xml</value>
<value>Course.hbm.xml</value>
…
</list>
</property>
…
</bean>
Now you have fully configured your sessionfactory ,so we need do create an object which we
will access hibernate. As we know, we will use a template class
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="courseDao" class="com.springinaction.
training.dao.hibernate.CourseDaoHibernate">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
2 Accessing Hibernate through HibernatTemplate
The template-callback mechanism in Hibernatee is pretty simple.There is the HibernatTmpplate and one callback interface
public Student getStudent(final Integer id) {
return (Student) hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
return session.load(Student.class, id);
}
});
The HibernateTemplate class provides some convience methods that implicit create a HibernateCallback instance:
(Student) hibernateTemplate.load(Student.class, id);
hibernateTemplate.update(student);
hibernateTemplate.find("from Student student " +
"where student.lastName = ?",
lastName, Hibernate.STRING);
3 Subclassing HibernateDaoSupport
public class StudentDaoHibernate extends HibernateDaoSupport
implements StudentDao {
…
}
getHibernateTemplate()
getSession()