简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM