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
<hataTable value='${teacher.students}' var='student' ...>
<hutputText value='${student.name}'/>
</hataTable>
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![]()
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,
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
In the rare cases where this is not true, I have alternate "load" methods that take a "loading strategy" argument indicating resolving all associations (in theory, the loading strategies could include other more "intermediate" states). I generally do this via JPQL fetch joins, but lazy loading would work too. I prefer JPQL for performance as well as the fact that I am very comfortable with SQL-like code. I really don't see ORM as a query language replacement; I see it more as a way of getting rid of a lot of boilerplate code (this is also the view of most persistence experts I talk to). In fact, the feedback many developers give me is that they almost prefer JPQL to some of the other functions in the entity manager API.
*Indeed, both lazily load data even when the entity manager instance is out of scope.* There was some talk in the JPA 2 spec to better standardize this behavior properly, don't know if this went anywhere...especially given the aforementioned "alpha geeks" that seem to know something the rest of us don't :-(.
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|