Welcome!

Java Authors: Maureen O'Gara, Michael Sheehan, Jonny Defh, Suresh Krishna Madhuvarsu, RealWire News Distribution

Related Topics: Java, Websphere

Java: Article

Cover Story: What Is POJO Programming?

Introducing POJO application development

EJB 3 Transaction Management
In EJB 3, declarative transaction management is, as you might expect, one of the built-in features of session and message-driven beans. You configure transaction management using either Java 5 annotations or a more traditional EJB deployment descriptor. This means, for example, that the MoneyTransferService EJB shown in Listing 2 will automatically be transactional.

Now that we've looked at how to make POJOs transactional and persistent let's examine the dangers of using annotated POJOs.

Beware of Annotated POJOs
The examples in this article use both XML and annotations to configure POJOs. However, the trend in POJO programming is away from XML configuration files and towards annotations. Annotations are certainly convenient because they're physically next to the program element (class, field, method) that they configure. The metadata is more concise and easier to maintain. There's certainly nothing wrong with using application-specific annotations. But, the problem with framework-specific annotations is that they couple your application to the framework in the same way that an API call does (www.aspectprogrammer.org/blogs/adrian/2004/08/ when_is_a_pojo.html).

For example, if you use Spring's @Transactional attribute then your classes won't compile without Spring. Similarly, if you use EJB 3 annotations then your classes will be coupled to the EJB 3 framework. These annotations won't stop you from testing your code with simple JUnit tests, but are classes that use them still POJOs?

Moreover, other annotations such as EJB 3's @EJB annotation, which injects an EJB, let you write code that's difficult to test without running it in the EJB container.

class SomeServiceImpl {
    @EJB private AccountDAO accountDAO;...
}

The @ EJB annotation tells the EJB container to "magically" initialize the private field on creation. Not a very POJO-like operation. Similarly, other frameworks' annotations couple your code to the database schema.

These kinds of annotations are definitely handy but using them will couple your application to the framework. That makes it difficult to support multiple frameworks simultaneously. Migrating to a later version or to a new framework can be difficult because you might have to change all of the annotations. This isn't quite as bad as rewriting the code but you still have to change the Java source and most importantly, you have to recompile. If you want to decouple your application from evolving infrastructure frameworks then you might want to consider using pure POJOs and XML metadata.

Summary
Developing with POJOs and non-invasive frameworks such as Spring, Hibernate, JDO, and EJB 3 provide many benefits. POJOs simplify and accelerate development. They make it easier to test classes in isolation without deploying them in an application server, a process that often slows down the edit-compile-debug cycle. And because POJOs are decoupled from the infrastructure frameworks that provide services such as transaction management and persistence you can focus on developing the business logic and address those concerns later.

POJOs also make it easier to upgrade your application to newer version of a framework or switch to a different framework entirely. They give your application increased immunity from the problems caused by rapidly evolving infrastructure frameworks. POJOs enable us to build Enterprise Java applications that last.

References

Acknowledgements
I would like to thank Chris Smith, Michael Yuan, Jennifer Shi, David Vydra, and Robert Benson for commenting on this article.

More Stories By Christopher Richardson

Chris Richardson is the author of the recently published POJOs in Action. He's a developer, architect, and mentor with over 20 years of experience. Chris runs a consulting company that jumpstarts new development projects and helps teams that are frustrated with Enterprise Java to become more productive and successful. He lives in Oakland, CA with his wife and three children.

Comments (8) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Preet 06/24/08 01:42:08 PM EDT

interesting read... answers the what, but, and what ifs..
Thanks! I am newbie in this domain and this is exactly what I was looking for.

Mathan 02/26/08 10:39:21 AM EST

The main advantage of the POJOs are felt in distributed applications. Assume that your application is consuming services from 10 different applications. If one of them is down, then your application is effectively down. Instead, if you have the POJOs for all those apps bundled in your app, then your application can stand alone without any dependency

rogerv 03/28/06 05:20:31 PM EST

What Is POJO Programming?

Oh - that's where you write 30% of a Java application in a declarative, XML-based domain specific language.

For all practical purposes (and by very definition) the domain specific language will be unique to a given POJO container and hence will render the Java application essentially unportable.

Patrick Ellul 03/14/06 10:25:04 PM EST

I found this article very helpful with introducing me to the concepts behind POJO's and ORM's and frameworks such as Spring and Hibernate.

Justin Peck 03/13/06 12:28:09 AM EST

I liked this article so much, I wrote a Web application after having read it. Spring really does make programming Java-base Web apps fun again. Thanks for the article!

[You can see the app now at http://javajuster.com]

Alain Ah Ming 02/26/06 05:21:30 AM EST

Is this simply replacing the underlying entity EJB with the value object?
If a J2EE app were designed in a way that we had distinct segregation as follows:
Session EJB --> BusinessLogic Helper class --ValueObj--> DAO interface --ValueObj--> DAO impl class --ValueObj--> Entity EJB

would that would make it as good as what POJO prog is trying to achieve?

From our test class, we can easily invoke our business logic helper class (which is a pure POJO) by injecting a test DAO impl class.
That effectively leaves out any dependency to the underlying framework (EJB or spring).

harris reynolds 02/23/06 05:39:06 PM EST

Trackback Added: JDJ RIP; I receive several trade rags about technolgy. Most of them get stacked up in a pile by my desk that I eventually go through after they start cluttering the office. When going through the latest round of magazines recently I...

SYS-CON Italy News Desk 02/21/06 01:40:28 PM EST

The novel A Deepness in the Sky by Vernor Vinge is set in the distant future. The character Pham Nuwen is responsible for maintaining software whose components are thousands of years old. Today, however, it's difficult to imagine maintaining an Enterprise Java application for more than a few years. More often than not, the application is tightly coupled to infrastructure frameworks that evolve rapidly in ways that don't preserve backwards compatibility. Consequently, upgrading to a new and improved framework can be challenging and risky.