数据模型的CDI生产者方法

发布于 2024-11-26 02:06:07 字数 910 浏览 0 评论 0原文

我希望能够 @Inject 支持 RichFaces 4 ExtendedDataTable 的数据模型,但它需要 EntityManager 来完成其工作。当然,EntityManager 的查询需要知道类,我不想将其传递到方法调用中(在这种情况下,我的代码不会调用这些方法);理想情况下它应该在构造函数中。

像这样的事情:

public class DataModel<T> {
    @Inject private EntityManager em;
    private Class<T> entityClass;

    public DataModel(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    //Sample method - this class will handle much more complex queries
    public T findEntity(String key) {
        return em.find(entityClass, key);
    }

是否可以创建一个可用于将此 DataModel 注入到我的支持 bean 中的 CDI @Producer?我考虑过制作一个限定符,这样你就可以做类似的事情

@Inject @JType(value = MyEntity.class) DataModel<MyEntity> dataModel;

但这看起来很笨拙,并且还需要我的 @Producer 调用 new() - 我认为这不允许注入 EntityManager进入数据模型。另外,我不确定您如何要求开发人员添加限定符。

或者也许有更好的方法来解决这个问题,而我错过了一些东西?

I'd like to be able to @Inject a data model backing a RichFaces 4 ExtendedDataTable, but it requires an EntityManager to do its work. The EntityManager's queries need to know the Class, of course, and I'd rather not pass that into method calls (in this case the methods are not called by my code); ideally it would be in the constructor.

Something like this:

public class DataModel<T> {
    @Inject private EntityManager em;
    private Class<T> entityClass;

    public DataModel(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    //Sample method - this class will handle much more complex queries
    public T findEntity(String key) {
        return em.find(entityClass, key);
    }

Is it possible to create a CDI @Producer that can be used to inject this DataModel into my backing beans? I've thought about making a Qualifier so you can do something like

@Inject @JType(value = MyEntity.class) DataModel<MyEntity> dataModel;

But that seemed clumsy, and would also require my @Producer to call new() - which I think would not allow the EntityManager to be injected into the DataModel. Also I'm not sure how you would require the qualifier to be added by the developer.

Or perhaps there's a better way to approach this, and I'm missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-12-03 02:06:07

我使用 seam-persistence< /a>来自seam3的模块。 :

Producer :

public class EntityManagerProducer {

   @Produces
   @ExtensionManaged
   @ConversationScoped
   @PersistenceUnit(unitName = "yourUnitName")
   private EntityManagerFactory emf;
}

然后你可以@Inject实体管理器。

否则,有一个看起来很有前途的 DeltaSpike 项目(从未使用过)还没有)

I do this using the seam-persistence module from seam3. :

Producer :

public class EntityManagerProducer {

   @Produces
   @ExtensionManaged
   @ConversationScoped
   @PersistenceUnit(unitName = "yourUnitName")
   private EntityManagerFactory emf;
}

Then you can @Inject the entity manager.

Otherwise, there is the DeltaSpike project that seems promising (never used it yet)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文