6. 6
Hibernate/Object-Relational Mapping
Tools/techniques to store and retrieve objects from
a database
From the code perspective it behaves like a
virtual object database
A complete ORM solution provide:
Basic CRUD functionality
An Object-Oriented Query Facility
Mapping Metadata support
Transactional Capability
7. 7
Hibernate pros and cons
Pros:
No SQL coding required
Database vendor independent
Provides caching mechanisms
Cons:
Requires learning of quite a lot of stuff
Limits queries to HQL syntax
Debugging and performance tuning is sometimes quite
complected
Does not fit with legacy or complex Database.
9. 9
About iBATIS
Open source framework that reduces the complexity to
read/save Java objects from a database (persistence)
Decouples Java code from SQL
Avoids the necessity to use JDBC
It is NOT an object-relational mapper (such as
Hibernate)
Simplifies the developer’s life ☺
10. 10
iBatis Components
iBATIS usage is divided into two main components:
SQLMaps
Permits to read/save Java objects into relational
DBMS without using JDBC and without mixing
Java and SQL code
DAO <Spring framework DAO>
“Is an abstraction layer that hides the details of
your persistence solution and provides a common
API to the rest of your application”
Note: previous versions of iBatis had separate DAO component which is deprecated in the
latest version iBatis 3.
12. 12
Sql Mapping
SQL Mapping Maps objects to SQL statements
NOT Classes to Tables
Fully functional SQL via named statements
NOT generated SQL (although that’s possible)
For example...
13. 13
The Product Class
public class Product
{
private int id;
private String name;
private String description;
private BigDecimal cost;
private BigDecimal retail;
// ...getters/setters implied
}
14. 14
The SQL
<select id=“getProduct" parameterClass=“int”
resultClass="examples.domain.Product">
SELECT
PRODUCT_ID as id,
NAME,
DESCRIPTION,
COST,
RETAIL,
FROM PRODUCT
WHERE PRODUCT_ID = #id#
</select>
16. 16
public class ProductDAOImpl extends SqlMapClientDaoSupport
implements
ProductDao
{
@SuppressWarnings("unchecked")
public List<Product> getAllProducts(int id)
{
List<Product> productList =
getSqlMapClientTemplate().queryForList(
"getProduct" , id );
return productList;
}
.....
The Product DAO Implementation
17. 17
...
“WTF!! Do you mean we need to hand
code XML configurations?”
“Yeah!! That's true..”
...
18. 18
JDBC
Obtain the db Connection
Create the statement
Set the input parameters
Execute the statement
Create the result Collection
For each row fetched:
Create an object
Set object’s properties using
row’s colums
Add the object to the
Collection
Release resources
Close the Connection
Return the Collection
IBATIS
Obtain the SqlMap object
Prepare complex parameters
(optional)
Invoke the query passing the
parameters
Return the result
JDBC-iBatis Comparison
19. 19
5 Reasons to use iBatis
1. Works with any database that has a JDBC driver
(can re-use existing MySQL queries ) & You already
know SQL, why waste timelearning something else?
2. Supports Map, Collection, List and Primitive
Wrappers (Integer, String etc.)
3. Easy integration with Spring DAO.
4. Works well with legacy DB and complex relational
mappings between DB tables.
5. Supports complex object mappings (populating
lists, complex object models etc.), Transactions, Lazy
Loading or Join Mapping (1:1, 1:M, M:N), and caching.
20. 20
- Full power of real SQL
- Works with complex, enterprise, ERP or
even poorly designed databases
- Fully supports composite keys and
complex relationships
- Complete stored procedure support
- JavaBeans support
- Primitive wrappers (Integer, String,
Date etc.)
- HashMap, List, Collection, and array[]
- XML text and DOM Support
- Null value translation
- Auto-mapping bean properties to
columns
- Dynamic SQL using conditional XML
tags
- Caching and dependency management
- Centralized data source configuration
- Local and global (JTA) transaction
support
- Small footprint 300k (minimum)
- Minimal dependencies (only common-
logging required)
- Spring DAO Templates available
iBATIS SQL Mapping Framework
21. 21
Books:
iBATIS In Action - Clinton Begin (Manning)
Links for reference:
http://en.wikipedia.org/wiki/IBATIS
http://ibatis.apache.org/onlinehelp.html
http://www.javabeat.net/articles/52-spring-ibatis-integration-1.html
http://www.learntechnology.net/content/ibatis/spring_ibatis.jsp
Using-iBatis-SQL-Maps-for-Java-Data-Access