Inject session bean into Entity

i'm writing a JEE6 application using EJB3.1, CDI and the persistent domain object pattern. In some cases i need so have an instance of an session bean inside an entity (jpa2.0). Injecting it using @Inject or @EJB does not work. its always a null reference.

The only possible solution was to do a lookup via InitialContext. But this is really bad. Does any one knows a solution how to access a session bean from an entity?

I've read about stateless session bean factory but could'nt find any example.


Entities are created by you, and if you want to have beans injected into them, they should be created by the container.

Technically, you can have your entities managed by the CDI container, and instead of making new YourEntity use @Inject @New YourEntity entity . But this is a bit counter-intuitive. I haven't tried it.

This is a part of a greater discussion about anemic data model vs domain driven design. Ie whether your entities should be data-holders without any behaviour, or they should also have logic (and hide their state)

链接地址: http://www.djcxy.com/p/61414.html

上一篇: JPA在合并期间作为管理实体的一部分分离实体

下一篇: 将会话bean注入实体