Develop enterprise Java applications with POJOs in Action

news
Jul 3, 200610 mins

Avoid the problems associated with EJB by working with plain-old Java objects and lightweight frameworks

Chris Richardson has been developing enterprise Java applications for several years. In the beginning, he based these applications on servlets, JavaServer Pages, and Enterprise JavaBeans (EJB). Chris discovered that he could simplify development by using plain-old Java objects—POJOs. Although using POJOs made it possible to test code without having to wait for this code to deploy to a server (a significant source of frustration), the lack of POJO support by the servlet and EJB frameworks required Chris to “jump through a few hoops to use them.” After describing some of these hoops in an article, Chris learned of lightweight frameworks that use POJOs directly.

Readers told Chris to forget about using entity beans to manage an application’s persistence—frameworks that map objects to relational database tables are simpler. It did not take long for Chris to start working with two persistence technologies: JDO (Java Data Objects) and Hibernate. Now that the troublesome entity beans could be eliminated, there was still the matter of dealing with the problematic session beans. As with entity beans, session beans need to be deployed to the server, and that slows development. After reading a few articles on the Spring framework, Chris found a solution to the session bean problem.

Working with POJOs in the context of the lightweight JDO, Hibernate, and Spring frameworks let Chris experience enterprise Java development with much less frustration than when he worked with the Java EE EJB framework (Java Platform, Enterprise Edition, or Java EE, is Sun’s new name for J2EE). He decided to write a book that shares his POJO/lightweight framework knowledge, and teaches simpler and faster ways to write enterprise Java applications. The result POJOs in Action, published by Manning Publications in January 2006, focuses on pure POJO and lightweight framework design, a much better approach for many Java enterprise applications. In this article, I present my review of POJOs in Action.

POJOs in Action

POJOs in Action targets developers and architects experienced in developing enterprise Java applications in the context of Java EE’s EJB framework, and wanting to effectively use POJOs and lightweight frameworks to boost their productivity. However, if you are experienced in developing enterprise applications outside of this framework (perhaps using C++ and non-Java technologies), you should be able to understand much (if not all) of this book’s content.

POJOs in Action is organized into 13 chapters that are divided into four parts. Part 1’s two chapters overview POJOs and lightweight frameworks. Part 2’s five chapters present a combination of options for using POJOs and lightweight frameworks to effectively design applications. Other approaches for designing the business and database access tiers are taught by the three chapters in Part 3. The final part’s three chapters investigate important database-related issues often encountered when developing an enterprise Java application.

Overview of POJOs and lightweight frameworks

Chapter 1 focuses on the disillusionment with EJB. After providing a brief history of EJB, this chapter presents a typical EJB 2.0 application’s architecture, highlights the problems with EJB, delves into the shortcomings of procedural design (and why this practice is encouraged by Java EE) and the pain of EJB development, and explores EJB 3.0’s attempts to overcome these problems.

This chapter also focuses on POJO-based development, where you learn about the POJO emphasis on object-oriented design, the important benefits of using POJOs, persisting POJOs with Hibernate and JDO, eliminating data transfer objects, making POJOs transactional with Spring, configuring applications with Spring, and deploying a POJO application to the server—not necessarily a full-blown application server. I appreciated the author’s money transfer service example, which is based on POJOs, Spring, and Hibernate.

Chapter 2 emphasizes five design decisions for creating enterprise applications. These decisions help you choose among the lightweight POJO approach, the heavyweight EJB 2.0 approach (which is necessary for certain kinds of enterprise applications, as you discover in a later chapter), and the combination POJO/heavyweight approach that EJB 3.0 uses. Chapter 2 walks you through a scenario where a development team addresses each decision in terms of a Food to Go application’s use-cases. I found this scenario very helpful.

A simpler, faster approach

Chapter 3 looks at implementing business logic in terms of a domain model (also known as an object model), which is based on some problem domain that you wish to solve. After introducing you to the Domain Model design pattern—which shows you where the domain model fits into the overall application architecture, presents an example domain model based on Food to Go, and introduces the various roles played by classes in the domain model—this chapter walks you through the development of a domain model.

You first learn to identify those features of a problem domain that you can model with classes, attributes, and relationships. You next learn how to add behavior to the domain model. The author reveals his favorite approach for implementing behavior: test-driven development. You discover the importance of refactoring your code, the benefits of using JUnit to perform tests, and a way to simplify and speed up these tests by using mock objects. In closing, Chapter 3 develops a portion of the domain model for one of Food to Go’s use-cases.

Chapter 4 explores persisting a domain model in a repository (an object data store). Important concepts include mapping a domain model to a database, accessing the database with an object-relational mapping (ORM) framework, using Spring’s ORM support classes, and testing a persistence layer. I especially enjoyed the mapping section. After showing you how to map classes to tables, and object relationship mapping, this section shows you how to map inheritance, manage object lifecycles, and persist object identity.

Following its mapping discussion, Chapter 4 provides an overview of ORM frameworks. You learn why you do not want to persist objects yourself and explore key features of ORM frameworks. A generic discussion of an ORM framework wouldn’t be complete without a list of benefits and drawbacks; this chapter nicely addresses this topic. After the overview, Chapter 4 introduces Hibernate and JDO, and discusses each framework’s support for key ORM features.

Chapter 4 next looks at designing repositories with Spring. It teaches you how to implement JDO and Hibernate repositories with Spring’s ORM classes, and also teaches you how to make repositories easier to test. You are given various strategies for testing with and without an underlying database. As a bonus, Chris introduces ORMUnit, his JUnit extension that simplifies testing the O/R mapping and persistent objects. Chapter 4 also discusses performance tuning with JDO and Hibernate, and closes by showing you Food to Go’s database schema.

Chapters 5 and 6 discuss persisting a domain model with JDO 2.0 and Hibernate 3, respectively. Each chapter starts by examining various issues associated with each framework. For example, JDO requires each domain model class to have an object identity. Should this identity be application identity or datastore identity? And Hibernate requires you to determine which domain model classes are entities and which classes are components. These chapters also discuss using JDO or Hibernate to implement a repository, and performance tuning.

Chapter 7 explores the use of POJO facades to encapsulate business logic. The chapter begins with an example (from the Food to Go application) that illustrates a POJO facade. The facade handles requests from presentation tier components that implement a specific use-case and invokes the domain model developed earlier in Chapter 3. The facade also uses Spring’s transaction interceptor to begin a transaction, open a persistence framework connection to be used by the repositories, close the connection, and commit the transaction.

As in other chapters, Chapter 7 presents lists of benefits and drawbacks—one of this book’s strengths. To be specific, Chapter 7 points out the benefits (such as faster and easier development) and drawbacks (such as no support for transactions initiated by a remote client) to using a POJO facade. Moving on, this chapter explores several decisions you must make when designing the facade. The chapter then explores how to design the facade’s public interface, how to implement the facade in a test-driven manner, and how to deploy the facade via Spring.

Variations

Although it sounds like heresy, there are benefits to dispensing with the POJO facade and directly exposing the domain model to the presentation tier. Chapter 8 focuses on this Exposed Domain Model pattern. You discover this pattern’s benefits (such as less code to write and maintain) and drawbacks (lack of encapsulation, for example). You learn how to manage persistence framework connections via a Spring-based servlet filter. You also learn how to manage transactions, either with a servlet filter or with a Spring interceptor.

It sometimes does not make sense to focus on object-oriented design and ORM frameworks. Chapter 9 discusses when to implement business logic with a procedural design and access the database using iBATIS, which maps POJOs to SQL statements and is very convenient for executing these statements. You are shown how to develop procedural business logic starting from a use-case, and how to structure this logic so that it is easier to maintain. Chapter 9 closes by showing you how to access the database with Spring’s iBATIS support classes.

Due to their dissatisfaction with EJB, many Java developers have migrated to alternate frameworks like Spring, JDO, and Hibernate. This migration led Sun to evolve EJB to embrace many POJO and lightweight framework concepts. Chapter 10 provides an overview of EJB 3.0, pointing out its key improvements (EJB components are now POJOs, for example) and limitations (such as limited support for collections). It also shows you how to persist a domain model, implement a facade, and implement other design patterns (such as Transaction Script) with EJB 3.0.

Dealing with databases and concurrency

Chapter 11 discusses the implementation of search screens that allow users to enter search criteria and page through the matching results. This chapter presents three main design issues: implementing a paging mechanism, dynamically generating queries, and optimizing queries to improve performance. The chapter then talks about implementing dynamic paged queries with iBATIS, JDO, and Hibernate. You are next presented with design examples using JDO and Hibernate in the Food to Go application. Finally, you learn about native SQL queries.

Because enterprise applications often have multiple users and background tasks, multiple database transactions will occasionally attempt to access the same data at the same time—thus, the possibility for data corruption arises. Chapter 12 presents optimistic locking and pessimistic locking solutions to this problem. It shows how to use these solutions in the context of iBATIS/Java Database Connectivity, JDO, and Hibernate. It also shows how to use Spring to recover from concurrency failures.

The optimistic and pessimistic locking solutions are appropriate for use-cases involving single transactions without user input. For use-cases with multiple transactions, especially those where user input is involved—the so-called edit-style use-cases—you need to work with offline locking patterns. Chapter 13 wraps up this book by exploring the Optimistic Offline Lock and Pessimistic Offline Lock patterns. You are shown each pattern’s benefits and drawbacks, and explore these patterns in the context of iBATIS/JDBC, JDO, and Hibernate.

Conclusion

POJOs in Action introduces many interesting topics. Below is a list of topic questions to pique your curiosity:

  • How does optimistic locking work? How does pessimistic locking work?
  • What are DAOs and what are DTOs? What is wrong with EJB DTOs?
  • What are mock objects?
  • What is the Transaction Script pattern?
  • What kinds of tests do you run against the database, and what kinds of tests don’t require the database?
  • When does it not make sense to focus on object-oriented design and ORM frameworks?
  • Why does JDO’s datastore identity make the presentation tier more complicated?

You’ll find answers to these questions in the book.

Though I don’t have much experience with the Java EE architecture, I found POJOs in Action to be easy to read and hard to put down. This book taught me when to work with EJBs and when to work with POJOs and lightweight frameworks, which will prevent much future frustration when I finally embrace enterprise Java application development. POJOs in Action can benefit you as well.

Jeff Friesen is a freelance software developer and educator specializing in C, C++, and Java technology.