Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

Interview: Adam Bien Defends Java EE 5

  • submit to reddit

Author, speaker, Java champion, and NetBeans Dream Team member Adam Bien addresses common fallacies about Java EE 5. The major fallacy he touches on is Java EE 5's supposed complexity, pointing out that the problem is more closely related to the inherent complexities of distributed programming, where one needs to deal with database synchronization, concurrency management, and transaction handling. He argues that the Java EE 5 API's are actually relatively simple, especially since at the end of the day they encapsulate the very complexities that make distributed computing so complex. Finally, Adam also discusses the issues that need to be understood when entering the domain of distributed computing, as well as scenarios in which switching to Java EE 5 make sense.

Related to this, in his blog Adam recently underlined the simplicity of getting started with EJB 3 and the benefits of doing so, in the following steps:

  1. In the IDE you will have to point to a JAR containing the annotations @Stateless, @Local, @Entity, @Id. If you have the Reference Implementation installed (Glassfish), simply puy glassfish\lib\javaee.jar on the classpath. You will need a persistence provider as well. In case of toplink, there is only one jar (glassfish\lib\toplink-essentials.jar). IDEs with built in EE support already have everything you need. However, here I would prefer to develop "from scratch" an EJB.

  2. Start with the Entity class. Just create a class and put @Entity tag on it:

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;

    public class Book{

    private Long id;
    private String title;

    public Book() {

    public Book(String title) {
    this.title = title;

    The @Id @GeneratedValue annotations denote the id field as a primary key. An entity should contain a parameterless constructor as well.

  3. Setup a DataSource at the application. jdbc/sample already exists in every fresh Glassfish installation (so nothing to do here).

  4. Create an interface with CRUD methods:
    import javax.ejb.Local;

    public interface BookService {
    Book createOrUpdate(Book book);
    void remove(Book book);
    Book find(Object id);
  5. Create a class which implements this interface. You will be forced by a good IDE (probly not by vi or emacs) to implement this interface:
    public class BookServiceBean implements BookService {
    private EntityManager em;

    public Book createOrUpdate(Book book) {
    return em.merge(book);
    public void remove(Book book) {
    public Book find(Object id) {
    return em.find(com.abien.Book.class, id);
    The method merge creates or updates an entity, all other methods should be self-explanatory. Hint: you cannot remove not-attached entities - you have to find them first. This is the "Seek And Destroy" pattern :-).

  6. You have to create a small XML file. However - it will not grow:
      <persistence-unit name="sample" transaction-type="JTA">
    <property name="toplink.ddl-generation" value="create-tables"/>
    There is only one persistence-unit element with the name "sample". EJB 3 Dependency Injection works with the "Convention Over Configuration" principle. This allows us to keep the injection of the EntityManager very lean: if there is only one possibility - you have not to configure it.

  7. Compile everything and JAR (the persistence.xml into META-INF) the output (in Netbeans just "build", in Eclipse "Export -> JAR")

  8. Copy the JAR into the autodeploy folder of WLS 10 (bea10\user_projects\domains\YOUR_DOMAIN\autodeploy), or glassfish\domains\domain1\autodeploy in the case of Glassfish v2, or jboss-4.2.2.GA\server\default\deploy in case of JBoss

  9. Inspect the log files, you are done :-)


What have you gained?

  • It's threadsafe (in multicore environments as well)
  • Remoting: you can access the interface remotely
  • It's transactional - transactions are started for you
  • It's pooled - you can control the concurrency and prevent "denial of service" attacks.
  • It's monitored: and EJB have to be visible through JMX. Application servers provide additional monitoring services as well.
  • Dependency Injection just works - you can inject persistence, other beans, legacy pojos (I will cover this in some upcomings posts)
  • It's portable and so vendor-neutral. Deployment to different application servers just works
  • There is almost NO XML.
  • Its easily accessible (via DI), from Restful services, JSF, Servlets etc.
  • Clustering and security are beneficial as well - but not the main reason to use EJBs
  • EntityManager is injected in thread-save manner.
  • Transactions are managed for you - the EntityManager participates in the transactions (no additional setup etc. necessary)


Published at DZone with permission of its author, Geertjan Wielenga.