Java EE 6 Overview
For the past year or so I have been working on a web development project using Java. Like many Java projects, the project I was working on used 3rd party frameworks like Spring and Hibernate rather than utilizing the official Java EE stack. Many developers have turned to these frameworks because the official Java EE stack had been notoriously cumbersome and difficult to use. That is starting to change. Java EE 5 started moving Java EE in a new direction. Inspired by the ideologies of the 3rd party frameworks, the JCP, the standards community that works on the Java specification, began to make radical changes to the core of Java EE. These changes have continued in the latest iteration, Java EE 6.
Interested in comparing Java EE 6 to my experiences with Spring and Hibernate, I picked up a book on the topic and began playing around with some code. It was easy to see the influence of Spring and Hibernate in components such as JPA, Java EE’s ORM solution; a dependency injection component; and a focus on using POJOs. The purpose of this article will be to give a brief overview of the core Java EE 6 technologies.
Java Persistence API
The Java Persistence API (JPA) is Java EE’s ORM solution. It is meant to be a replacement for EJB Entity Beans, which have been deprecated in Java EE 6. If you are familiar with Hibernate, then you will feel right at home with JPA. In fact, many Hibernate developers played a role in the design and development of JPA. For those of you unfamiliar with Object-Relational Mapping (ORM); it is a technique used to map your programs domain objects to the tables of your database. ORMs usually make it very easy to perform CRUD operations on your data as well provide you with methods to query the underlying database using your object model rather than a standard SQL query. ORMs commonly abstract away the underlying database and present a clean, cross-database API for accessing data. JPA, like the latest versions of Hibernate, allow two methods of mapping your classes to the database: XML and annotations. Following the trends of the day, use of annotations is encouraged over XML, but both are fully supported. Using JPA is easy. You only have to create a single, simple XML file containing database connection information, place the appropriate JPA annotations on your, and then you are ready to go!
Enterprise JavaBeans (EJB) is Java EE’s business layer solution. The EJBs are where you will place most of your business logic. The EJB engine will take care of many cross cutting concerns such as transactions and security for you. EJB was heavily frowned upon in previous versions for being very heavy-weight and complicated. Starting with version 3, EJB received a complete makeover. It is now much more lightweight. You can work almost entirely with POJOs and no longer need to inherit from EJB classes. Like the rest of Java EE 6, EJB uses convention over configuration so the very little configuration you do have to do can be done using simple annotations. There are two main types of EJB: Session Beans and Message Driven Beans.
Session Beans themselves can be classified into three types: Stateless, Stateful, and Singleton. Stateless session beans are great for functionality that does not need to maintain state. For example, a bean that simply queries the database to received the latest news updates can be stateless. Because state does not need to be maintained, stateless beans can be kept in a pool and reused for multiple requests. Stateful Beans are good for situations where you need to maintain state, such as keeping track of a customers shopping cart. Stateful beans stay alive for the duration of the session. Finally, Singleton Beans can be used for logic that you want to have available globally. Only a single copy of Singleton bean is created and it is shared by all requests.
Message Driven Beans (MDB) are used to receive messages from Java Message Service (JMS). An MDB can subscribe to messaging queues and can act on messages that appear in the queue.
JavaServer Faces (JSF) is Java EE’s presentation technology. It is similar to ASP.NET Web Forms in that it uses controls, called components in JSF, that emit HTML code. Unlike ASP.NET Web Forms, however, JSF is an MVC framework. Previous versions of JSF used JSPs as the view engine. In Java EE 6, Facelets are the default view engine. Unlike JSPs, Facelets are valid XML files that work a lot better with the JSF component model. The model can be accessed through something called a managed bean. A managed bean is a simple POJO that is annotated as a managed bean. The properties of the managed bean can be used directly in the facelet and can be bound to the components.
The latest version of Java EE also provides dependency injection functionality. This makes your application more loosely coupled and also helps make unit testing easier. As mentioned earlier, Java EE 6 favors the use of annotations over XML configuration. The people who complained of “XML Hell” in previous versions of Java EE can breathe a sigh of relief. Convention over configuration has also taken a hold in the Java EE world. Instead of having developers create a ton of configuration, Java EE will default to using reasonable defaults and allow for configuration when you wish to override the defaults. This combined with the use of annotations over XML allows you to hit the ground running with Java EE faster than ever before!
Overall, I feel that Java EE 6 is a huge step in the right direction. I think JPA is great and very comparable to Hibernate. EJB has been improved a lot and is now much easier to use. As far as JSF goes, I prefer to use a framework like Spring MVC with JSPs as the view engine. I think that the JSP view engine is very powerful and I like working close to the HTML rather than abstracting it away. Others may definitely enjoy the power of JSF though. I feel that dependency injection in Java EE is a little lacking compared to what is available in Spring. I would say that if you are working on an existing project using Spring or another third party framework, you probably shouldn’t bother changing over to Java EE 6. However, if you are starting out on a new project, I would take a look at Java EE 6. It is powerful and much easier to use now, having learned a lot from frameworks like Spring and Hibernate.
Want to learn more?
I recommend the book Beginning Java EE 6 Platform with Glassfish 3 by Antonio Goncalves. It is well written and gives a great overview of development with Java EE 6.