• Applications can use the JDBC API to access data residing in a relational database management system
(RDBMS).
• However, even within an RDBMS environment, the actual syntax and format of the SQL statements
may vary depending on the particular database product.
• There is even greater variation with different types of persistent storage (flat files, xml, LDAP).
• Components such as bean-managed entity beans, session beans, servlets, and other objects like helpers
for JSP pages need to retrieve and store information from persistent stores.
• But including the connectivity and data access code within these components introduces a tight coupling
between the components and the data source implementation.

So the requirement is
• Components need to be transparent to the actual persistent store or data source implementation to
provide easy migration to different vendor products, different storage types, and different data source
types.

Approach

• Use a Data Access Object (DAO) to abstract and encapsulate all access to the data source. The DAO
manages the connection with the data source to obtain and store data.

Understanding DAO


Hibernate Intro

Hibernate Intro

• Now this is clear that we will be having multiple DAO and TO classes.
• One more thing if we are designing to support multiple databases then these DAO must be created for
each type of database separately, rather then putting if-else inside the code.
• So, here it is a clear need of factory method and abstract factory pattern.

So let’s start–
All databases specific DAO must implement same set of methods, to ensure this better to create interfaces
that will be implemented by DAO classes.

Hibernate Intro

Same thing we will do for customer, order, account, department etc. We will provide a db specific factory class
that will provide underlying DAOs.

Hibernate Intro

All database specific DAO factories must provide their specific DAO, so they all must have same set of
methods. Again need of some abstraction. We can use either interface or abstract class.

Hibernate Intro

Hibernate Intro

So, now it should be very much clear that, what DAO pattern is and when it is used through factory method and
abstract factory pattern it helps us to support multiple databases comfortably.