Data Access Object
Data Access Object
• hibernate-mapping default-lazy="false">
•
• <class name=«Project.UsersRole" table="L_ROLE" >
• <id name="id" type="long" column="ID" >
• <generator class="org.hibernate.id.TableHiLoGenerator">
• <param name="table">IKM_SEQ</param>
• <param name="column">SEQUENCE_NO</param>
• <param name="max_lo">0</param>
• </generator>
• </id>
• <property name="name" type="string" column="NAME" length="60" not-null="true"/>
• <property name="exp" type="string" column=«EXPLANATION" length="255"/>
• </class>
• </hibernate-mapping>
Implementing Data Access Object pattern
Searching for a particular role name
• Criteria criteria;
• criteria = session.createCriteria(Role.class);
• if (role.getName() != null && rol.getName().length() != 0)
• criteria.add(Restrictions.ilike("name",
• "%" + rol.getName() + "%"));
• criteria.addOrder(Order.asc("name"));
• results = criteria.list();
Results
• Enables Transparency
– Business objects can use the data source without knowing the specific
details of the data source's implementation. Access is transparent because
the implementation details are hidden inside the DAO.
• Enables Easier Migration
– A layer of DAOs makes it easier for an application to migrate to a different
database implementation. The business objects have no knowledge of the
underlying data implementation. Thus, the migration involves changes
only to the DAO layer.
• Reduces Code Complexity in Business Objects
– Because the DAOs manage all the data access complexities, it simplifies
the code in the business objects and other data clients that use the DAOs.
All implementation-related code (such as SQL statements) is contained in
the DAO and not in the business object. This improves code readability
and development productivity.
Results
• Centralizes All Data Access into a Separate Layer
– Because all data access operations are now delegated to the DAOs, the separate data access layer can be
viewed as the layer that can isolate the rest of the application from the data access implementation. This
centralization makes the application easier to maintain and manage.
• Not Useful for Container-Managed Persistence
– Because the EJB container manages entity beans with container-managed persistence (CMP), the container
automatically services all persistent storage access. Applications using container-managed entity beans do
not need a DAO layer, since the application server transparently provides this functionality. However, DAOs
are still useful when a combination of CMP (for entity beans) and BMP (for session beans, servlets) is
required.
• Adds Extra Layer
– The DAOs create an additional layer of objects between the data client and the data source that need to be
designed and implemented to leverage the benefits of this pattern. But the benefit realized by choosing
this approach pays off for the additional effort.
• Needs Class Hierarchy Design
– When using a factory strategy, the hierarchy of concrete factories and the hierarchy of concrete products
produced by the factories need to be designed and implemented. This additional effort needs to be
considered if there is sufficient justification warranting such flexibility. This increases the complexity of the
design.
Referances
• http://
java.sun.com/blueprints/corej2eepatterns/Pa
tterns/DataAccessObject.html
• http://
en.wikipedia.org/wiki/Data_access_object
• http://en.wikipedia.org/wiki/Object-
relational_mapping