Design Generic CRUD Session Bean

后端 未结 2 510
臣服心动
臣服心动 2021-01-03 09:44

This question has been asked once here EJB 3 Session Bean Design for Simple CRUD, and I just want to ask more in depth questions about this design. I already tried to ask th

相关标签:
2条回答
  • 2021-01-03 09:58

    1.- Your stateless bean is exposing the interface GenericCrudService (the view is what the application server registers), for that reason you use

    @EJB
    private GenericCrudService myEJB;
    

    If you have multiple implementations of this interface, you can add the ejb name to the annotation for disambiguation:

    @EJB(name="GenericCrudServiceBean")
    private GenericCrudService myEJB;
    

    In EJB 3.1, Session Bean without interface are exposing something called a No-Interface View. These session beans can be used only locally.

    0 讨论(0)
  • 2021-01-03 10:20
    1. The EJB container will actually create a proxy-based implementation of the interface that wraps your bean and adds things like transactions and security checks. This proxy is then injected into the annotated field, which is why it needs to have the interface type. I think in EJB 3.1, you can omit the interface and have only a bean class, and the container will create a subclass to implement its magic.
    2. javax.ejb.Remote is an EJB 3.0 annotation, while java.rmi.Remote is a plain interface from before EJBs existed. There is no javax.rmi.Remote. And you are right, if you do not use the Remote annotation, then by default the EJB will be considered to have only a local interface.
    3. From the API doc of TransactionAttributeType.MANDATORY:

      If a client invokes the enterprise bean's method while the client is associated with a transaction context, the container invokes the enterprise bean's method in the client's transaction context.

      If there is no existing transaction, an exception is thrown.

      You probably want to use TransactionAttributeType.REQUIRED, which automatically starts a transaction if none is present and is also the default if you use the annotation without a parameter.

    0 讨论(0)
提交回复
热议问题