[Dolenc] Hibernate vs. iBatis

19

Click here to load reader

Transcript of [Dolenc] Hibernate vs. iBatis

Page 1: [Dolenc] Hibernate vs. iBatis

HIBERNATE VS. IBATIS

OR TWO WAYS OF DATABASE ACCESS

BOŠTJAN DOLENC, MARAND INŽENIRING

Page 2: [Dolenc] Hibernate vs. iBatis

WHAT WE'RE ABOUT TO SEE:

• A bit about relevancy

• The ORM way (with Hibernate examples)

• The JDBC+ way (with iBatis examples)

• Problems

• Crossing the chasm

• Discussion (time permitting)

Page 3: [Dolenc] Hibernate vs. iBatis

RELEVANCY

• Data matters more than code

• JDBC sucks

– Aimde at system development

– Lacks higher level features: statement

manipulation, encapsulation, caching

– Cumbersome use

• Objects ≠ Relations (holy war)

Page 4: [Dolenc] Hibernate vs. iBatis

ORM WAY

• Object/class centric approach

• Strict Object-Relational Mapping

or

“I hate bloody SQL”

• Using Hibernate 3.x (and JDK <1.5)

Page 5: [Dolenc] Hibernate vs. iBatis

ORM EXAMPLE

Page 6: [Dolenc] Hibernate vs. iBatis

ORM EXAMPLE - CONTINUED

Page 7: [Dolenc] Hibernate vs. iBatis

JDBC+ WAY

• SQL centric approach

• Improved JDBC

or

“Ad-hoc queries without JDBC horrors”

Page 8: [Dolenc] Hibernate vs. iBatis

JDBC+ EXAMPLE

Page 9: [Dolenc] Hibernate vs. iBatis

JDBC+ EXAMPLE - CONTINUED

Page 10: [Dolenc] Hibernate vs. iBatis

ORM PROBLEM: 1+N FETCHING

1

2

10 1

11 1

12 1

13 2

42 10

43 11

44 11

45 12

46 13

22 42

23 42

... ...

Page 11: [Dolenc] Hibernate vs. iBatis

ORM PROBLEM: 1+N FETCHING - CONTINUED

• Eager loading to the rescue:

– Next-level-only fails in deep hierarchies

– All-levels fails when accessing only top-level

• HQL to the rescue:

– from Customer c

join c.contracts co

join co.bundles b

join b.services s

Page 12: [Dolenc] Hibernate vs. iBatis

ORM PROBLEM: MASSIVE UPDATES

1 1.1.

2 1.2. 1.12.

3 15.10.

4 1.2.

5 1.1 1.12.

for (ServiceTimeline st : services)

{

if (st.getValidTo() == null)

{

st.setValidTo(newDate);

}

}

update service_timeline set valid_to =

#newDate# where valid_to is null

Page 13: [Dolenc] Hibernate vs. iBatis

OTHER ORM PROBLEMS:

• xQL (HQL, JP QL) ≠ SQL

– Tools

– Limited to programmers

• POJOs sometimes aren’t Plain and Old

– Common base class, proxies, code

generation, annotations...

– Build/startup overhead, subtle reflection bugs

Page 14: [Dolenc] Hibernate vs. iBatis

JDBC+ PROBLEM: LOTS OF WORK FOR SIMPLE THINGS

Page 15: [Dolenc] Hibernate vs. iBatis

JDBC+ PROBLEM: LOTS OF WORK FOR SIMPLE THINGS - CONTINUED

Page 16: [Dolenc] Hibernate vs. iBatis

JDBC+ PROBLEM: MANUAL SAVING (INSERT/UPDATE) IS TEDIOUS

1

2

10 1

11 1

12 1

13 2

42 10

43 11

44 11

45 12

46 13

22 42

23 42

... ...

1

2

3

4

5

Page 17: [Dolenc] Hibernate vs. iBatis

OTHER JDBC+ PROBLEMS:

• Limited caching

– Caches only query results (by definition)

– ORM can “understand” mutations, JDBC+

can’t

• Fear of SQL

– People problem

– Training and tools help a lot

Page 18: [Dolenc] Hibernate vs. iBatis

MIX APPROACHES TO CROSS THE CHASM:

From ORM toward JDBC+

• Eager flushing

• HQL, SQL queries

• Form-based / non-generic

development

• Discipline, examine the

inner workings

From JDBC+ toward ORM

• Save phase at the end

• Get-by-id, update, insert,

delete for each class

• Active Record-like DAOs,

reflection + dynamic SQL

• Custom development

Page 19: [Dolenc] Hibernate vs. iBatis

CONCLUSION

• The ORM-SQL chasm might be scary

• It’s not that wide

• You can stay in the middle with some

tricks