如何在EntityListener中使用EJB? [英] How can I use an EJB in a EntityListener?

查看:140
本文介绍了如何在EntityListener中使用EJB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计数据库,但遇到了实体本身需要访问数据库的情况.

I'm designing my database and I reached the situation that an Entity itself requires to access database.

我阅读了为什么有时需要在JPA实体内部引用EntityManager .

我很好奇在EntityListener中访问EJB的可能性.

And I'm curious about the possibility of accessing an EJB in EntityListener.

public class MyEntityListener {

    @PrePersist
    private void onPrePersist(final Object object) {
        // find an EJB
        // and set those required values
        // which each resides in a specific table.
    }
}

这是首选还是(或)和?

Is this possible or (or and) preferred?

推荐答案

我找到了答案.

  • CDI injection in EntityListeners
  • https://blogs.oracle.com/arungupta/entry/jpa_2_1_early_draft

根据 JSR 338:JavaTM Persistence 2.1 ,这似乎是可能的.

It seems possible, according to JSR 338: JavaTM Persistence 2.1.

3.5.1实体侦听器

在启用CDI的情况下,Java EE环境中的实体侦听器类通过上下文和依赖注入API(CDI)[10]支持依赖注入.利用CDI注入的实体侦听器类还可以定义带有PostConstructPreDestroy注释的生命周期回调方法.这些方法将在注入发生之后和实体侦听器实例分别销毁之前被调用.

Entity listener classes in Java EE environments support dependency injection through the Contexts and Dependency Injection API (CDI) [ 10 ] when CDI is enabled[43]. An entity listener class that makes use of CDI injection may also define lifecycle callback methods annotated with the PostConstruct and PreDestroy annotations. These methods will be invoked after injection has taken place and before the entity listener instance is destroyed respectively.

持久性提供程序负责使用CDI SPI创建实体侦听器类的实例;在这种情况下进行注射;调用其PostConstructPreDestroy方法(如果有);并处理实体侦听器实例.

The persistence provider is responsible for using the CDI SPI to create instances of the entity listener class; to perform injection upon such instances; to invoke their PostConstruct and PreDestroy methods, if any; and to dispose of the entity listener instances.

这篇关于如何在EntityListener中使用EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆