POJOs in Action
Developing Enterprise Applications with Lightweight Frameworks
Chris Richardson
  • January 2006
  • ISBN 9781932394580
  • 592 pages

Chris Richardson has done an outstanding job; this book deserves 5 out of 5. I wish I could have given more. Once I started reading the book, it was hard for me to put it down.

Meera Subbarao, JavaLobby.org

There is agreement in the Java community that EJBs often introduce more problems than they solve. Now there is a major trend toward lightweight technologies such as Hibernate, Spring, JDO, iBATIS, and others, all of which allow the developer to work directly with the simpler Plain Old Java Objects, or POJOs. Bowing to the new consensus, EJB 3 now also works with POJOs.

POJOs in Action describes these new, simpler, and faster ways to develop enterprise Java applications. It shows you how to go about making key design decisions, including how to organize and encapsulate the domain logic, access the database, manage transactions, and handle database concurrency.

Table of Contents show full



about this book

about the title

about the cover illustration

Part 1 Overview of POJOs and lightweight frameworks

1. Developing with POJOs: faster and easier

1.1. The disillusionment with EJBs

1.1.1. A brief history of EJBs

1.1.2. A typical EJB 2 application architecture

1.1.3. The problems with EJBs

1.1.4. EJB 3 is a step in the right direction

1.2. Developing with POJOs

1.2.1. Using an object-oriented design

1.2.2. Using POJOs

1.2.3. Persisting POJOs

1.2.4. Eliminating DTOs

1.2.5. Making POJOs transactional

1.2.6. Configuring applications with Spring

1.2.7. Deploying a POJO application

1.2.8. POJO design summary

1.3. Summary

2. J2EE design decisions

2.1. Business logic and database access decisions

2.2. Decision 1: organizing the business logic

2.2.1. Using a procedural design

2.2.2. Using an object-oriented design

2.2.3. Table Module pattern

2.3. Decision 2: encapsulating the business logic

2.3.1. EJB session façade

2.3.2. POJO façade

2.3.3. Exposed Domain Model pattern

2.4. Decision 3: accessing the database

2.4.1. What’s wrong with using JDBC directly?

2.4.2. Using iBATIS

2.4.3. Using a persistence framework

2.5. Decision 4: handling concurrency in database transactions

2.5.1. Isolated database transactions

2.5.2. Optimistic locking

2.5.3. Pessimistic locking

2.6. Decision 5: handling concurrency in long transactions

2.6.1. Optimistic Offline Lock pattern

2.6.2. Pessimistic Offline Lock pattern

2.7. Making design decisions on a project

2.7.1. Overview of the example application

2.7.2. Making high-level design decisions

2.7.3. Making use case-level decisions

2.8. Summary

Part 2 A simpler, faster approach

3. Using the Domain Model pattern

3.1. Understanding the Domain Model pattern

3.1.1. Where the domain model fits into the overall architecture

3.1.2. An example domain model

3.1.3. Roles in the domain model

3.2. Developing a domain model

3.2.1. Identifying classes, attributes, and relationships

3.2.2. Adding behavior to the domain model

3.3. Implementing a domain model: an example

3.3.1. Implementing a domain service method

3.3.2. Implementing a domain entity method

3.3.3. Summary of the design

3.4. Summary

4. Overview of persisting a domain model

4.1. Mapping an object model to a database

4.1.1. Mapping classes

4.1.2. Mapping object relationships

4.1.3. Mapping inheritance

4.1.4. Managing object lifecycles

4.1.5. Persistent object identity

4.2. Overview of ORM frameworks

4.2.1. Why you don’t want to persist objects yourself

4.2.2. The key features of an ORM framework

4.2.3. Benefits and drawbacks of using an ORM framework

4.3. Overview of JDO and Hibernate

4.3.1. Declarative mapping between the object model and the schema

4.3.2. API for creating, reading, updating, and deleting objects

4.3.3. Query language

4.3.4. Support for transactions

4.3.5. Lazy and eager loading

4.3.6. Object caching

4.3.7. Detached objects

4.3.8. Hibernate vs. JDO

4.4. Designing repositories with Spring

4.4.1. Implementing JDO and Hibernate repositories

4.4.2. Using the Spring ORM classes

4.4.3. Making repositories easier to test

4.5. Testing a persistent domain model

4.5.1. Object/relational testing strategies

4.5.2. Testing against the database

4.5.3. Testing without the database

4.5.4. Overview of ORMUnit

4.6. Performance tuning JDO and Hibernate

4.6.1. Without any tuning

4.6.2. Configuring eager loading

4.6.3. Using a process-level cache

4.6.4. Using the query cache

4.7. The example schema

4.8. Summary

5. Persisting a domain model with JDO 2.0

5.1. JDO issues and limitations

5.1.1. Configuring JDO object identity

5.1.2. Persisting interfaces

5.1.3. Using the JDO enhancer

5.2. Persisting a domain model class with JDO

5.2.1. Writing JDO persistence tests with ORMUnit

5.2.2. Testing persistent JDO objects

5.2.3. Making a class persistent

5.3. Implementing the JDO repositories

5.3.1. Writing a mock object test for findRestaurants()

5.3.2. Implementing JDORestaurantRepositoryImpl

5.3.3. Writing the query that finds the restaurants

5.3.4. Writing tests for a query

5.4. JDO performance tuning

5.4.1. Using fetch groups to optimize object loading

5.4.2. Using a PersistenceManagerFactory-level cache

5.4.3. Using a query cache

5.5. Summary

6. Persisting a domain model with Hibernate 3

6.1. Hibernate ORM issues

6.1.1. Fields or properties

6.1.2. Hibernate entities and components

6.1.3. Configuring object identity

6.1.4. Using the cascade attribute

6.1.5. Persisting interfaces

6.2. Other Hibernate issues

6.2.1. Exception handling

6.2.2. Lazy loading and inheritance hierarchies

6.3. Persisting a domain model class using Hibernate

6.3.1. Writing Hibernate persistence tests with ORMUnit

6.3.2. Testing persistent Hibernate objects

6.3.3. Making a class persistent

6.4. Implementing a repository using Hibernate

6.4.1. Writing a mock object test for a repository method

6.4.2. Implementing HibernateRestaurantRepositoryImpl

6.4.3. Writing the query that finds the restaurants

6.4.4. Writing tests for a query

6.5. Hibernate performance tuning

6.5.1. Using eager loading

6.5.2. Using a process-level cache

6.5.3. Using a query cache

6.6. Summary

7. Encapsulating the business logic with a POJO façade

7.1. Overview of a POJO façade

7.1.1. An example POJO façade

7.1.2. Benefits of a POJO façade

7.1.3. Drawbacks of a POJO façade

7.1.4. When to use a POJO façade and detached domain objects

7.2. POJO façade design decisions

7.2.1. Encapsulating the domain objects

7.2.2. Detaching objects

7.2.3. Exceptions versus status codes

7.2.4. Managing transactions and connections

7.2.5. Implementing security

7.2.6. Supporting remote clients

7.3. Designing a POJO façade’s interface

7.3.1. Determining the method signatures

7.4. Implementing the POJO façade

7.4.1. Writing a test for a POJO façade method

7.4.2. Implementing updateRestaurant()

7.5. Implementing a result factory

7.5.1. Implementing a Hibernate result factory

7.5.2. Implementing a JDO result factory

7.6. Deploying the POJO façade with Spring

7.6.1. Generic bean definitions

7.6.2. JDO-specific bean definitions

7.6.3. Hibernate bean definitions

7.7. Summary

Part 3 Variations

8. Using an exposed domain model

8.1. Overview of the Exposed Domain Model pattern

8.1.1. Applying the Exposed Domain Model pattern

8.1.2. Benefits and drawbacks of this pattern

8.1.3. When to use the Exposed Domain Model pattern

8.2. Managing connections using a Spring filter

8.3. Managing transactions

8.3.1. Managing transactions in the presentation tier

8.3.2. Managing transactions in the business tier

8.4. An example of the Exposed Domain Model pattern

8.4.1. Servlet design

8.4.2. JSP page design

8.4.3. PlaceOrderService configuration

8.5. Using JDO with an exposed domain model

8.5.1. Defining the Spring beans

8.5.2. Configuring the web application

8.6. Using Hibernate with an exposed domain model

8.6.1. Defining the Spring beans

8.6.2. Configuring the web application

8.7. Summary

9. Using the Transaction Script pattern

9.1. Overview of the Transaction Script pattern

9.1.1. Applying the Transaction Script pattern

9.1.2. Benefits and drawbacks of the Transaction Script pattern

9.1.3. When to use the Transaction Script pattern

9.2. Identifying the transaction scripts

9.2.1. Analyzing the use case

9.2.2. Analyzing the user interface design

9.2.3. The PlaceOrderTransactionScripts interface

9.3. Implementing a POJO transaction script

9.3.1. Writing a test for the transaction script

9.3.2. Writing the transaction script

9.4. Implementing the DAOs with iBATIS and Spring

9.4.1. Overview of using iBATIS with Spring

9.4.2. Implementing a DAO method

9.5. Configuring the transaction scripts using Spring

9.5.1. How Spring manages JDBC connections and transactions

9.5.2. The Spring bean definitions

9.6. Summary

10. Implementing POJOs with EJB 3

10.1. Overview of EJB 3

10.1.1. Key improvements in EJB 3

10.1.2. Key limitations of EJB 3

10.2. Implementing a domain model with EJB 3

10.2.1. Mapping the classes to the database

10.2.2. Implementing repositories

10.2.3. Testing the persistent EJB domain model

10.3. Implementing a façade with EJB 3

10.3.1. Turning a POJO façade into a session bean

10.3.2. Detaching objects

10.4. Assembling the components

10.4.1. Using EJB dependency injection

10.4.2. Integrating Spring and EJB dependency injection

10.4.3. Using Spring dependency injection

10.5. Implementing other patterns with EJB 3

10.5.1. Implementing the Exposed Domain Model pattern

10.5.2. Implementing the Transaction Script pattern

10.5.3. Implementing dynamic paged queries

10.5.4. Implementing the concurrency patterns

10.6. Summary

Part 4 Dealing with databases and concurrency

11. Implementing dynamic paged queries

11.1. Key design issues

11.1.1. Implementing a paging mechanism

11.1.2. Generating queries dynamically

11.1.3. Improving the performance of SQL queries

11.2. Implementing dynamic paged queries with iBATIS

11.2.1. Using queryForList() to select the rows

11.2.2. Using ROWNUM to select the rows

11.3. Implementing paged queries with JDO and Hibernate

11.3.1. Generating Hibernate and JDO queries dynamically

11.3.2. Loading the data with a single SELECT statement

11.3.3. Loading a subset of an object’s fields

11.3.4. Working with a denormalized schema

11.3.5. Implementing paging

11.4. A JDO design example

11.4.1. The JDOOrderRepositoryImpl class

11.4.2. The ExecuteFindOrdersQuery class

11.5. A Hibernate design example

11.5.1. The HibernateOrderRepositoryImpl class

11.5.2. The FindOrdersHibernateCallback class

11.6. Using JDO and Hibernate native SQL queries

11.6.1. Using JDO native SQL queries

11.6.2. Using Hibernate SQL queries

11.7. Summary

12. Database transactions and concurrency

12.1. Handling concurrent access to shared data

12.1.1. Using fully isolated transactions

12.1.2. Optimistic locking

12.1.3. Pessimistic locking

12.1.4. Using a combination of locking mechanisms

12.2. Handling concurrent updates in a JDBC/iBATIS application

12.2.1. Design overview

12.2.2. Using optimistic locking

12.2.3. Using pessimistic locking

12.2.4. Using serializable or repeatable read transactions

12.2.5. Signaling concurrent update failures

12.3. Handling concurrent updates with JDO and Hibernate

12.3.1. Example domain model design

12.3.2. Handling concurrent updates with JDO

12.3.3. Handling concurrent updates with Hibernate

12.4. Recovering from data concurrency failures

12.4.1. Using an AOP interceptor to retry transactions

12.4.2. Configuring the AOP interceptor

12.5. Summary

13. Using offline locking patterns

13.1. The need for offline locking

13.1.1. An example of an edit-style use case

13.1.2. Handling concurrency in an edit-style use case

13.2. Overview of the Optimistic Offline Lock pattern

13.2.1. Applying the Optimistic Offline Lock pattern

13.2.2. Benefits and drawbacks

13.2.3. When to use this pattern

13.3. Optimistic offline locking with JDO and Hibernate

13.3.1. Using version numbers or timestamps

13.3.2. Using detached objects

13.4. Optimistic offline locking with detached objects example

13.4.1. Implementing the domain service

13.4.2. Implementing the persistent domain class

13.4.3. Detaching and attaching orders

13.5. The Pessimistic Offline Lock pattern

13.5.1. Motivation

13.5.2. Using the Pessimistic Offline Lock pattern

13.5.3. Benefits and drawbacks

13.5.4. When to use this pattern

13.6. Pessimistic offline locking design decisions

13.6.1. Deciding what to lock

13.6.2. Determining when to lock and unlock the data

13.6.3. Choosing the type of lock

13.6.4. Identifying the lock owner

13.6.5. Maintaining the locks

13.6.6. Handling locking failures

13.7. Using pessimistic offline locking in a domain model

13.7.1. Implementing a lock manager with iBATIS

13.7.2. Implementing the domain service

13.7.3. Adapting the other use cases

13.8. Summary



What's inside

  • Leverage the frameworks’ strengths, avoid their weaknesses
  • Apply enterprise patterns in the lightweight world
  • New patterns like POJO Façade and Exposed Domain Model
  • Build rich domain models
  • How Aspects improve design
  • Lightweight testing strategies
  • How to be agile

About the reader

Written for developers and designers, this is a new-generation Java applications guide. It helps you build lightweight applications that are easier to build, test, and maintain. The book is uniquely practical with design alternatives illustrated through numerous code examples.

About the author

Chris Richardson is a developer and architect with over 20 years of experience. His consulting company specializes in jumpstarting projects and mentoring teams. Chris has been a technical leader at Insignia, BEA, and elswhere. He has a computer science degree from the University of Cambridge in England and lives in Oakland, CA.

eBook $35.99 PDF only

FREE domestic shipping on three or more pBooks

A good way to quickly get up to speed with today's practices for lightweight development.

Floyd Marinescu, TheServerSide.com

Brings back simplicity to enterprise Java applications.

Jonas Bonér, Terracotta, Inc.

A valuable guide for lightweight development.

Craig Walls, Author of Spring in Action

The author definitely knows what he is talking about.

Oliver Zeigermann, J2EE Architect and Apache committer

Extremely valuable, plenty of sample code... I enthusiastically recommend it!

Brendan Murray, Senior Software Architect, IBM