简体   繁体   English

将会话bean注入实体

[英]Inject session bean into Entity

i'm writing a JEE6 application using EJB3.1, CDI and the persistent domain object pattern. 我正在使用EJB3.1,CDI和持久域对象模式编写JEE6应用程序。 In some cases i need so have an instance of an session bean inside an entity (jpa2.0). 在某些情况下,我需要在实体(jpa2.0)中有一个会话bean的实例。 Injecting it using @Inject or @EJB does not work. 使用@Inject或@EJB注入它不起作用。 its always a null reference. 它始终是空引用。

The only possible solution was to do a lookup via InitialContext. 唯一可能的解决方案是通过InitialContext查找。 But this is really bad. 但这真的很糟糕。 Does any one knows a solution how to access a session bean from an entity? 有谁知道一种解决方案,如何从实体访问会话bean?

I've read about stateless session bean factory but could'nt find any example. 我已经阅读了有关无状态会话bean工厂的信息,但是找不到任何示例。

Entities are created by you, and if you want to have beans injected into them, they should be created by the container. 实体是由您创建的,如果您想将bean注入其中,则应由容器创建它们。

Technically, you can have your entities managed by the CDI container, and instead of making new YourEntity use @Inject @New YourEntity entity . 从技术上讲,您可以让您的实体由CDI容器管理,而不是使new YourEntity使用@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) 也就是说,您的实体应该是没有任何行为的数据持有者,还是应该具有逻辑(并隐藏其状态)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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