access existing instance stateful inside stateless, java ee 6

后端 未结 4 1098
旧巷少年郎
旧巷少年郎 2020-12-06 03:41

Is it possible to access a stateful session bean inside a stateless bean?

My problem is that I have a session bean called User and I want to access user info inside

4条回答
  •  抹茶落季
    2020-12-06 04:16

    In case the others weren't clear enough already: You're doing it wrong! ;)

    I agree that it might be confusing, but the only session in EJB session beans is kept in the proxy bean that you obtain from the InititalContext.

    Different beans that you obtain from this context do not share any kind of common session. In EJB, beans are not stored IN an EJB session but they ARE this session.

    In other words, the InitialContext (ctx in your code) is NOT the EJB equivalence for the HttpSession.

    Even worse perhaps is that in your code the User IS an EJB bean. This is WRONG.

    A user is a noun in your application. These are represented by JPA entities or simple 'normal' java beans. EJBs are for implementing the verbs in your application: Services, DAOs, Repositories, etc.

    The state in stateful session beans is supposed to hold on to model data during a business process ( for caching, locking, reservations, etc purposes). In no situation should this state BE the model data.

    My advice: let your current 'design' go. Don't try to fix it, don't try to justify it. Let it go, delete your code, don't look back. Read some good books about EJB and then start over.

    Good luck!

提交回复
热议问题