52
Clinton R. Begin [email protected]

Clinton R. Begin [email protected]. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin [email protected]

Embed Size (px)

Citation preview

Page 1: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

Clinton R. [email protected]

Page 2: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

2

Dealing with Enterprise Database Challenges

Featuring the Apache iBATIS Data Mapping Framework

Clinton [email protected]

Page 3: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

3

Demo…

Just move data from here to there.

Page 4: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

4

ScopeThe Challenges

• Ownership, access, complexity, normalization, skinny design.

iBATIS Data Mapping Framework

• Introduction, SQL Mapping defined, examples.

SQL

• Is it still relevant?

iBATIS is a hybrid solution

• Features, qualities, competition, other solutions.

Page 5: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

5

The Challenges

“Welcome, to the real world.”

– go for the red pill

Page 6: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

6

Challenges of PoliticsDatabase ownership and control

Change management

Cost allocation – who pays for changes?

No “developer” access to design

Proprietary/3rd Party – legal issues

Agile methods vs. legacy methods

Page 7: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

7

iBATIS isolates the data model, the SQL, the work and the responsibility. In doing so, it isolates much of the politics surrounding the enterprise database as well.

Page 8: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

8

Challenges of ImperfectionDenormalized models

Super-normalized models

Modeled Value Entities

Thin data models (rows vs. columns)

Implicit relationships

Overcomplicated relationships

Null Values (?)

Page 9: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

9

Thin data model

Group Name Type Value

1 Street String 12 Some St

1 City String Modesto

1 State String California

1 Zip String 12345

2 Street String 543 Other St

2 City String Fresno

2 State String California

2 Zip String 34332

Page 10: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

10

iBATIS works even when the data model does not map to the object model.

Page 11: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

11

Challenges of Legacy and complexityToo many tables to map

Encoded Fields

No primary keys

Hierarchical

ERP systems

Temporal databases

“600 tables” – moderate size (?)

Page 12: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

12

iBATIS allows you to build modern applications on top of legacy databases by allowing you more freedom to define and tune your mappings that deal with “unique” databases.

Page 13: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

13

Challenges of TechnologyDistributed transactions

Distributed caching

Vendor specific database features

Multiple databases w/ single object model

Page 14: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

14

iBATIS supports advanced enterprise features and allows you to take full advantage of the investment you’ve made in your relational database management system.

Page 15: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

15

An Introduction to Apache iBATIS

SQL Mapping defined…

Page 16: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

16

SQL Mapping

Concept

Page 17: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

17

SELECT FIRST_NAME, LAST_NAME

FROM EMPLOYEE

WHERE EMPLOYEE_NUMBER = ‘1234’

INPUT

OUTPUT OUTPUT

SQL as a Black Box

Page 18: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

18

SQL MappingMaps objects to SQL statements• NOT Classes to Tables

Fully functional SQL via named statements• NOT generated SQL (although that’s possible)

For example…

Page 19: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

19

id : inttotal : BigDecimalpst : BigDecimalgst : BigDecimllineItems : Collectiondate : Date

Order

id : intproduct : Productorder : Ordercost : BigDecimalretail : BigDecimal

LineItem id : intname : Stringdescription : Stringcost : BigDecimalretail : BigDeciml

Product

Order

LineItemProduct

Mapping Layer

TABLES

CLASSES

Page 20: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

20

id : inttotal : BigDecimalpst : BigDecimalgst : BigDecimllineItems : Collectiondate : Date

Order

id : intproduct : Productorder : Ordercost : BigDecimalretail : BigDecimal

LineItem id : intname : Stringdescription : Stringcost : BigDecimalretail : BigDeciml

Product

Order

LineItemProduct

Mapping Layer

SQL

TABLES

CLASSES

Page 21: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

21

The Product Table

PRODUCT

PRODUCT_ID INTEGER PK, NN

NAME VARCHAR NN

DESCRIPTION TEXT NN

COST MONEY NN

RETAIL MONEY NN

Page 22: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

22

The Product Class

public class Product {

private int id;

private String name;

private String description;

private BigDecimal cost;

private BigDecimal retail;

//…getters/setters implied

}

Page 23: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

23

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>

Page 24: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

24

Simple Configuration

String resource = “SqlMapConfig.xml”;

Reader reader = Resources

.getResourceAsReader (resource);

SqlMapClient sqlMap = SqlMapClientBuilder

.buildSqlMapClient(reader);

Page 25: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

25

Executing the Query

Product product =

(Product) sqlMap.queryForObject

(“getProduct”, 5);

Page 26: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

26

…Audience: OMG! Did you just hand code SQL?

Clinton: Yes.…

Page 27: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

27

SQL

Is it still relevant?

Page 28: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

28

SQLStructured Query Language

Introduced in 1973 by IBM

Based on relational model of 1970

Based on sound mathematical principles

Significant industry investment

Has withstood the test of time

Nothing else has ever come close

Page 29: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

29

“SQL is much more than a simple data update and retrieval mechanism. SQL's query processing can perform many tasks. By hiding SQL, application developers are excluding a powerful tool.”

http://www.martinfowler.com/articles/dblogic.html

Page 30: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

30

Writing SQL is NOT a Sin!

Page 31: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

31

Is SQL what we want to avoid?

SELECT * FROM EMPLOYEE

WHERE EMPLOYEE_NUMBER = 99

Page 32: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

32

Or is it JDBC? public Employee getEmployee (int id) throws

SQLException {

Employee employee = null;

String sql = "SELECT * FROM EMPLOYEE " +

"WHERE EMPLOYEE_NUMBER = ?";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = dataSource.getConnection ();

ps = conn.prepareStatement(sql);

ps.setInt(1, id);

rs = ps.executeQuery();

employee = null;

while (rs.next()) {

employee = new Employee();

employee.setId (rs.getInt("EMP_ID"));

employee.setEmployeeNumber (rs.getInt("EMP_NUMBER"));

employee.setFirstName (rs.getString("EMP_FIRST_NAME"));

employee.setLastName (rs.getString("EMP_LAST_NAME"));

employee.setTitle (rs.getString("EMP_TITLE"));

}

} finally {

try {

if (rs != null) rs.close();

} finally {

try {

if (ps != null) ps.close();

} finally {

if (conn != null) conn.close();

}

}

}

return employee;

}

Page 33: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

33

iBATIS is a hybrid solution

Qualities, features and competition.

Page 34: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

34

Options…Stored procedures

Inline SQL

Dynamic SQL

Object Relational Mapping

iBATIS IS A HYBRID!

Page 35: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

35

A Hybrid?Full support for stored procedures

SQL is written just like inline SQL• “Inline SQL for XML”

Advanced dynamic SQL definition features• A big problem even for the best ORM tools

Shares many features with ORM• Caching, lazy loading, join mapping, bytecode

enhancement etc.

Page 36: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

36

Apache iBATIS…Is tolerant of complex/bad database designs

Isolates the data model

Separates concerns

Divides labor

Saves time

Reduces code

Page 37: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

37

Advanced iBATIS FeaturesSupports all types • Objects, Primitives, Arrays, Collections

Caching (use case vs. holistic)

Lazy Loading or Join Mapping (1:1, 1:M, M:N)

Bytecode enhancement (ifaces AND classes)

XML parameters and results

Transaction Management (Local/Global)

Page 38: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

38

But yes…SQL Coding is RequiredMany people consider this an advantage

Many others eventually realize it’s required

Few people consider it a problem

SQL can be done fast and done well• Use good tools (there are plenty)

• Generation is an option (but not a best practice)

• The SQL may already exist (consider app rewrites or

ports)

Page 39: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

39

Other “SQL Mapper-Like” Tools JDBC PreparedStatement

Embedded SQL as a language feature• Pro*C, Forte TOOL, SQLJ

Spring framework

Voruta

SQLC (iBATIS inspired?)

O/R Broker (iBATIS inspired)

Mr. Persister (iBATIS inspired)

Aximol SQL Library (iBATIS inspired)

Page 40: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

40

iBATIS 3 Years agoJPetStore posted on TSS

iBATIS noticed in the persistence layer

Architectural reviewer said: “Use Torque”

Why not just use ORM for everything?

What kept it going…?

Page 41: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

41

What drives open source?

Frustration / Anger / Need

Confidence / Ego

Pride

Duty

Community

Page 42: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

42

iBATIS Today~10,000 users

~1.2 Million DTD hits per month from tools

Apache Software Foundation

12 Developers

Java, .NET, Ruby

Page 43: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

43

Audience Response

Questions?

Page 44: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

44

More simple examples…To inspire questions or support answers.

Page 45: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

45

<select id="getProduct" parameterClass=“int” resultClass=“com.domain.Product">

select PRODUCT_ID as id, NAME as name, DESCN as description, CATEGORY as categoryfrom PRODUCT where PRODUCTID = #id#

</select>

Page 46: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

46

<resultMap id="productResult“

class="com.domain.Product">

<result property="id" column="PRODUCTID"/>

<result property="name" column="NAME"/>

<result property="description" column="DESCN"/>

<result property="categoryId" column="CATEGORY"/>

</resultMap>

<select id="getProduct“

parameterClass=“int”

resultMap="productResult">

select * from PRODUCT

where PRODUCTID = #id#

</select>

Page 47: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

47

<parameterMap id="productParameter“ class=“int"> <parameter property=“id"/></parameterMap>

<select id="getProduct“ parameterMap=“productParameter” resultClass=“com.domain.Product“>

select

PRODUCT_ID as id,

NAME as name,

DESCN as description,

CATEGORY as category

from PRODUCT

where PRODUCTID = ?

</select>

Page 48: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

48

<statement id=“insertProduct“

parameterClass=“com.domain.Product”>

insert into PRODUCT

values (#id#,#name#,#description#,#category#)

</statement>

<statement id=“updateProduct“

parameterClass=“com.domain.Product”>

update PRODUCT set

NAME = #name#,DESC = #description#,CATEGORY=#category#

where PRODUCT_ID = #id#

</statement>

<statement id=“deleteProduct“

parameterClass=“com.domain.Product”>

delete PRODUCT where PRODUCT_ID = #id#

</statement>

Page 49: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

49

Simple Query

Product product =(Product) sqlMap.queryForObject

(“getProduct”, 23);

Page 50: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

50

Insert, Update, DeleteProduct product = new Product();

product.setId(324);

product.setName(“Shih Tzu”);

Product.setDescription(“Some longer description.”);

product.setCategory(“DOG”);

sqlMap.insert(“insertProduct”, product);

product.setCategory(“CAT”);

sqlMap.update(“updateProduct”, product);

product.delete(“deleteProduct”, product);

Page 51: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

51

Transaction Handlingtry {

sqlMap.startTransaction();

sqlMap.insert(“insertProduct”, product);

sqlMap.update(“updateProduct”, product);

product.delete(“deleteProduct”, product);

sqlMap.commitTransaction();

} finally {

sqlMap.endTransaction();

}

Page 52: Clinton R. Begin cbegin@apache.org. 2 Dealing with Enterprise Database Challenges Featuring the Apache iBATIS Data Mapping Framework Clinton Begin cbegin@thoughtworks.com

52

What does an application using SQL Maps look like?

See JPetStore 4