From dc83107d662f812a9e3463db00b5ea0167647db1 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Fri, 8 May 2009 20:33:32 +0000 Subject: [PATCH] replaced *DaoSuport coverage with annotations used for DAOs --- spring-framework-reference/src/dao.xml | 258 +++++++++++++------------ 1 file changed, 135 insertions(+), 123 deletions(-) diff --git a/spring-framework-reference/src/dao.xml b/spring-framework-reference/src/dao.xml index 669111932b..aa9e7818ac 100644 --- a/spring-framework-reference/src/dao.xml +++ b/spring-framework-reference/src/dao.xml @@ -1,128 +1,140 @@ - +"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> - DAO support -
- Introduction - - The Data Access Object (DAO) support in Spring is aimed at - making it easy to work with data access technologies like - JDBC, Hibernate or JDO in a consistent way. This allows one - to switch between the aforementioned persistence technologies - fairly easily and it also allows one to code without worrying - about catching exceptions that are specific to each technology. - -
-
- Consistent exception hierarchy - - Spring provides a convenient translation from technology-specific - exceptions like SQLException to its own - exception class hierarchy with the - DataAccessException as the root exception. - These exceptions wrap the original exception so there is never - any risk that one might lose any information as to what might - have gone wrong. - - - In addition to JDBC exceptions, Spring can also wrap Hibernate-specific - exceptions, converting them from proprietary, checked exceptions - (in the case of versions of Hibernate prior to Hibernate 3.0), to - a set of focused runtime exceptions (the same is true for JDO and - JPA exceptions). This allows one to handle most persistence exceptions, - which are non-recoverable, only in the appropriate layers, without - having annoying boilerplate catch-and-throw blocks and exception - declarations in one's DAOs. (One can still trap and handle exceptions - anywhere one needs to though.) As mentioned above, JDBC exceptions - (including database-specific dialects) are also converted to the - same hierarchy, meaning that one can perform some operations with - JDBC within a consistent programming model. - - - The above holds true for the various template classes in Springs - support for various ORM frameworks. If one uses the interceptor-based - classes then the application must care about handling - HibernateExceptions and - JDOExceptions itself, preferably via delegating - to SessionFactoryUtils' - convertHibernateAccessException(..) or - convertJdoAccessException methods respectively. - These methods convert the exceptions to ones that are compatible - with the exceptions in the org.springframework.dao - exception hierarchy. As JDOExceptions are - unchecked, they can simply get thrown too, sacrificing generic DAO - abstraction in terms of exceptions though. - - - The exception hierarchy that Spring provides can be seen below. - (Please note that the class hierarchy detailed in the image - shows only a subset of the entire - DataAccessException hierarchy.) - - - - - - -
-
- Consistent abstract classes for DAO support - - To make it easier to work with a variety of data access technologies - such as JDBC, JDO and Hibernate in a consistent way, Spring provides - a set of abstract DAO classes that one can extend. - These abstract classes have methods for providing the data source and - any other configuration settings that are specific to the relevant - data-access technology. - - - - - JdbcDaoSupport - superclass for JDBC data - access objects. Requires a DataSource - to be provided; in turn, this class provides a - JdbcTemplate instance initialized from the - supplied DataSource to subclasses. - - - - - HibernateDaoSupport - superclass for - Hibernate data access objects. Requires a - SessionFactory to be provided; - in turn, this class provides a - HibernateTemplate instance initialized - from the supplied SessionFactory - to subclasses. Can alternatively be initialized directly via a - HibernateTemplate, to reuse the latters - settings like SessionFactory, - flush mode, exception translator, and so forth. - - - - - JdoDaoSupport - super class for JDO data - access objects. Requires a - PersistenceManagerFactory - to be provided; in turn, this class provides a - JdoTemplate instance initialized from the - supplied PersistenceManagerFactory - to subclasses. - - - - - JpaDaoSupport - super class for JPA data - access objects. Requires a - EntityManagerFactory to be provided; - in turn, this class provides a JpaTemplate - instance initialized from the supplied - EntityManagerFactory to subclasses. - - - -
+ DAO support + +
+ Introduction + + The Data Access Object (DAO) support in Spring is aimed at making it + easy to work with data access technologies like JDBC, Hibernate or JDO in + a consistent way. This allows one to switch between the aforementioned + persistence technologies fairly easily and it also allows one to code + without worrying about catching exceptions that are specific to each + technology. +
+ +
+ Consistent exception hierarchy + + Spring provides a convenient translation from technology-specific + exceptions like SQLException to its own exception + class hierarchy with the DataAccessException as the + root exception. These exceptions wrap the original exception so there is + never any risk that one might lose any information as to what might have + gone wrong. + + In addition to JDBC exceptions, Spring can also wrap + Hibernate-specific exceptions, converting them from proprietary, checked + exceptions (in the case of versions of Hibernate prior to Hibernate 3.0), + to a set of focused runtime exceptions (the same is true for JDO and JPA + exceptions). This allows one to handle most persistence exceptions, which + are non-recoverable, only in the appropriate layers, without having + annoying boilerplate catch-and-throw blocks and exception declarations in + one's DAOs. (One can still trap and handle exceptions anywhere one needs + to though.) As mentioned above, JDBC exceptions (including + database-specific dialects) are also converted to the same hierarchy, + meaning that one can perform some operations with JDBC within a consistent + programming model. + + The above holds true for the various template classes in Springs + support for various ORM frameworks. If one uses the interceptor-based + classes then the application must care about handling + HibernateExceptions and + JDOExceptions itself, preferably via delegating to + SessionFactoryUtils' + convertHibernateAccessException(..) or + convertJdoAccessException methods respectively. + These methods convert the exceptions to ones that are compatible with the + exceptions in the org.springframework.dao exception + hierarchy. As JDOExceptions are unchecked, they can + simply get thrown too, sacrificing generic DAO abstraction in terms of + exceptions though. + + The exception hierarchy that Spring provides can be seen below. + (Please note that the class hierarchy detailed in the image shows only a + subset of the entire DataAccessException + hierarchy.) + + + + + + +
+ +
+ Annotations used for configuring DAO or Repository classes + + The best way to guarantee that your Data Access Objects (DAOs) or + repositories provide exception translation is to use the + @Repository annotation. This annotation + also allows the component scanning support to find and configure your DAOs + and repositories without having to provide XML configuration entries for + them. + + @Repository +public class SomeMovieFinder implements MovieFinder { + + // ... + +} + + Any DAO or repository need to access to a persistence resource, + depending on the persistence technology used. The easiest way to + accomplish this is to have this resource dependency injected using one of + the @Autowired, + @Resource or + @PersistenceContext annotations. Here is an + example for a JPA repository: + + + + If you are using the classic Hibernate APIs than you can inject the + SessionFactory: + + + + Last example we will show here is for typical JDBC support. You + would have the DataSource injected into an + initialization method where you would create a + JdbcTemplate and other data access support classes + like SimpleJdbcCall etc using this + DataSource. + + +
\ No newline at end of file