An Oracle blog about Java Technology

EJB 3 series by Adam Bien

Guest Author

Adam Bien photo

Java Champion, Adam Bien has been running the following series of blogs around EJB 3 in the past few days -

Why I like EJB 3.0 and really like EJB 3.1, ...

Heavy EJBs, lightweight POJOs, ....

What happens, if you start with EJB3.x ...

Adam makes several points around the "lightweightness" of EJB's both at development time and runtime, contrasts the EJB3 approach with similar yet different technologies like Spring or Guice, and takes the upcoming EJB 3.1 improvements into account concluding that EJB's should be considered as no more than "midweight". Overall this three-part set of posts is a very nice read with fact-based opinions.

Adam Bien is an independent consultant and a member of the several JCP Expert Groups (EJB 3.1, Java EE 6, ...) and will be presenting on GlassFish v2 and v3 at Jazoon next week in Zurich, Switzerland.

Join the discussion

Comments ( 3 )
  • Thai Dang Vu Wednesday, June 18, 2008

    I read all 3 articles

    • Why I like EJB 3.0 and really like EJB 3.1, ...

    • Heavy EJBs, lightweight POJOs, ....

    • What happens, if you start with EJB3.x ...

    and didn't see where you compare a part of EJB3 with Spring (I say 'a part of EJB3' because I heard that EJB3 covers something like JCA that Spring doesn't).

    I came to EJB3 from Spring and there are a couple of things that are bugging me (I'm not good at EJB3 and not good at Spring either, so I'm very thankful if any of you can give me solutions for the following problems I'm facing):

    - In web-based applications: Spring has OpenEntityManagerInViewFilter. What is the counterpart in EJB3? If you never use Spring and don't know what OpenEntityManagerInViewFilter is, let me explain it in my own knowledge: for example, to display the information of all the students of a teacher in JSF, I write something like

    <h:dataTable value='${teacher.students}' var='student' ...>

    <h:outputText value='${student.name}'/>


    The OpenEntityManagerInViewFilter open an entity manager when this JSF page is rendered and doesn't close it until this JSF page finishes being rendered. So we won't have the lazy initialization exceptions.

    In EJB3, I have to eager loading all the students associated with a teacher in the JQL query or write the JQL like

    select t from teacher t left join fetch t.students

    or execute 'select t from teacher t' and t.getStudents().size().

    All 3 ways in EJB3 aren't flexible because sometimes I need the information of the teacher and his students but some other times I need the info of the teacher only.

    - In Spring, when I need to use caches, I have an aroundInvoke AOP class wrapping around a method that I want to provide caching with. A cache implementation (e.g. Ehcache), which is an instance of a class, can be injected into that AOP class. How can I inject an instance of a regular class into an EJB3 session bean? Which annotation should I use for that injection (@Resource?)? How can I create just 1 instance of that class and use that instance everywhere in the application? I'm thinking of creating that instance when the application starts, put it in the JNDI context and use JNDI lookup to retrieve it whenever I need it. Is that a good way to do it or is there any better way?

    If you read from the beginning to this sentence, then thank you very much for your time of reading my comment :)

  • Adam Bien Thursday, June 19, 2008

    Hi Thai Dang Vu,

    1. I actually didn't compared EJB 3 with Spring, I just described EJB 3 :-) from my point of view. What you are searching for is "extended" PersistenceContext in a @Stateful session bean. Then all Entities remain attached and are lazy loadable.

    2. You can use Interceptors (seach for @Interceptor) - very similar concept to AOP for

    decorating EJBs.

    3. There are no Singletons in EJB 3.0 right now. They will be available in EJB 3.1 - but for other purposes (startup, configuration, global cache).

    If you only would like to keeep your entities attached and usable outside the EM without eager loading, then all you need are stateful session beans with "EXTENDED" EntityManager.

    thanks for your comment,


  • Alexis MP Thursday, June 19, 2008

    Right, it was a bit of a stretch for me to call this a comparison.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.