: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
• Applications can use the JDBC API to access data residing in a relational database management system
• 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
• 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.
• 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.
Same thing we will do for customer, order, account, department etc. We will provide a db specific factory class
that will provide underlying DAOs.
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.
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.
• A DAO can use an ORM framework for persistence tasks.
• ORM framework is written to reduce the efforts done by DAO classes, like database connectivity,
constructing sql queries etc.
• ORM also improve the performance of DAO layer by providing caching, query optimization etc.
• Even ORM take care of database independency that’s why we do not require using factory method and
abstract factory pattern at DAO layer for achieving the same.
• DAO was 2 step approach, ORM will be introduced as a 3rd step, see the figure below.
• ORM will provide n number of features that we will see shortly.
There are other options also TopLink, iBatis, JDO, EJB-Entity Beans etc but hibernate is much more powerful
and simple to use.
We want to persist a record that keep the information about a employee through Hibernate.
Step 1 – Entity class
need to be persisted --> Employee.java ~= EmployeeTO.java
Step 2 –
Hibernate requires a mapping file
(employee.hbm.xml), that keep the information like table belongs to
this employee class, column mapping and association of this entity with other existing entities.Step 3 –
framework configuration file (hibernate.cfg.xml
) keeping the information about the database
connection properties, mapping files locations of entities created in step 2 etc.Step 4 – client program
where we will take Hibernate’s SessionFactory and Session then will call save(),
update() etc. method to perform persistency.