4

AFAIK, the whole purpose of JBoss Seam is to integrate EJB and JSF.

The book Seam in Action says that:

By design, EJB components cannot be bound directly to a JSF view. It’s great that EJB components are scalable, transactional, thread-safe, and secure, but it doesn’t do much good if they are completely isolated from the web tier, accessible only through a JSF backing bean acting as an intermediary.

But I couldn't find the reason/motive of such impossibility, why are they isolated from the web tier? Why can't I use an EJB as a backing bean? Care to enlighten me?

AndresQ
  • 803
  • 5
  • 19

2 Answers2

2

But I couldn't find the reason/motive of such impossibility, why are they isolated from the web tier?

Traditionally this was done to enforce the separation between business logic and view related code. Intermixing those is what most every beginner does and it always leads to systems that are hard to maintain in the long run.

By allowing EJBs to be directly used as backing beans, people are going to put FacesMessages in them, and formatters specific for one particular page, and rendering code and so on.

Strict isolation prevents these mistakes to be made. However, this also raises the bar for those very same beginners to get started with EJB and gave EJB the reputation of being somewhat difficult (although with EJB3 this is not really the case). In the latest Java EE 6 release, a lot of intermixing is made possible by allowing EJBs to be defined without interfaces in the web module. With CDI annotations they are directly useable as backing beans.

Depending on your view, this is a step forward (easier, less constraints, less required artifacts), but it could also be seen as a step backward (promotes mixing of view and business logic again, something which deprecating scriptlets on JSP tried to prevent).

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • but is there something that really prevents me from using an @Entity as a backing bean or is it just a bad practice? I remember reading something about not being able to access both the web state (request, session) and transactional state at the same time – AndresQ Jan 27 '11 at 14:24
  • An @Entity as backing bean??? That would be a bit awkward :P But you can perfectly well bind to entities if this is what you mean with "backing bean". Each view (page) usually has a single backing bean that fetches data from a Service. The result can be a collection of entities or a single entity. The backing bean returns this and the view can bind to it. E.g. h:input value="#{myBacking.person}" where person is an @Entity. – Arjan Tijms Jan 27 '11 at 19:30
  • Why would it be awkward? it's what seam encourages – AndresQ Jan 28 '11 at 10:49
  • I wouldn't really say seam (CDI now) *encourages* it. It's being made possible to associate entities with a scope, but that's more intended to do data binding and not really as a backing bean that contains e.g. the save/update etc methods. Maybe the confusion is between managed bean (any 'helper' bean used by EL on a page) and a true backing bean (a kind of managed bean that 'controls' the entire functionality for a single page). – Arjan Tijms Jan 28 '11 at 22:22
0

Seam is not aimed to do that. CDI (Weld) is, however. With CDI you can @Inject an EJB in a JSF bean. (As arjan noted - you can do this without CDI as well, and since CDI is JavaEE6, you can try using @EJB private RemoteInterface bean)

But this is not about JPA entities. Although JPA considered part of EJB, and its predecessors were "Entity beans", a special type of EJBs, it is best not to think of EJB and JPA as one think. JPA is an ORM standard. EJB is a service/component model.

@Entity objects are managed by an EntityManager. They are not managed by an ejb container, nor by seam, nor by CDI even. This is because they don't fit the component model these three employ. Entities are usually instantiated by the developer. With CDI/EJB/Seam it is the framework that instantiates the components, manages them and performs injection.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    >With CDI you can inject an EJB in a JSF bean - injecting an EJB in a JSF Managed Bean is also possible In Java EE 5 or 6 without CDI ;) – Arjan Tijms Jan 26 '11 at 22:33
  • @arjan yes - do I remember correctly that it is done by `@Resource(name="jndiName")` ? If so, let me clarify my answer that cdi gives injection by type, without the need to specify jndi names. – Bozho Jan 26 '11 at 22:35
  • It's even simpler than that, just `@EJB SomeBeanInterface someBean`. – Arjan Tijms Jan 26 '11 at 22:44
  • @arjan - aha. thanks for the clarification. Hadn't used EJB for more than an year now :) – Bozho Jan 26 '11 at 22:48
  • `Seam is not aimed to do that` Can you clarify? – Shervin Asgari Jan 27 '11 at 16:13
  • well, seam is a dependency injection framework meant to ease the work with web technologies. But it is not particularly interested in injecting EJBs - it is possible, as noted, even without seam. – Bozho Jan 27 '11 at 19:43