I am experimenting with Jakarta EE and Jakarta Faces (JSF). I just made a CDI named session scoped bean (as JSF managedBean's are deprecated now), and was wondering why one would use a stateful EJB when scoped beans (running in the CDI container) are available, it seems to me that any transactions can be done using a stateless bean injected into the CDI managed bean.
Any real-world use cases would be really helpful :).
For those wondering, my Managed/named bean looks like this:
import jakarta.ejb.EJB;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Named;
@Named("userBean")
@SessionScoped
public class UserSessionBean implements java.io.Serializable{
@EJB
TransactionBean bean; //can be used to persist user data
String username;
String password;
// Constructor, getters and setters
}
why one would use a stateful EJB when scoped beans (running in the CDI container) are available
Practically speaking almost never. Stateful Enterprise Beans were never intended as (scoped) backing beans anyway, and even when EJB was still recommended to be used, the stateful variation had little usage in web applications.
Originally the stateful enterprise bean had more utility when using it as a remote component using CORBA/RMI (e.g., as a kind of binary Servlet).
There's a highly specialised and somewhat obscure use-case for them now when taking advantage of the extended persistence context in Jakarta Persistence (JPA). Somehow in over a decade we never managed to specify its behaviour for other beans than stateful session beans.