Design Generic CRUD Session Bean

后端 未结 2 507
臣服心动
臣服心动 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 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.

提交回复
热议问题