Search code examples
javajakarta-eeejbcdi

CDI Bean with executing JPA queries


Short question, shall a bean/instance in javaEE (wildfly, cdi) be @RequestScoped if its executing jpa queries inside (only selects) or shall I always use @stateless for jpa?


Solution

  • I don't get the point of the question.

    If you have a business layer and the possibility to use EJB I would go for a @Stateless holding business logic and JPA interaction, and exposing a specific business interface to client.

    In any case, if you are just doing a select and nothing else, maybe for filling your datatable on front end, I suggest to annotate your business method like this:

    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<MyEntity> findByWhateverCriteria(String param) {
      // business logic
    }
    

    why ? Because you can save application server resources.

    On the other side, a @RequestScoped is appropriate if, for the same reason above, you only need to display data on front end and forget about it.